2016-08-10 3 views
0

이것은 정말로 의문이 될 것입니다.하지만 저는 저의 삶을 알 수 없습니다. 나는 내가 사용할 수있는 올바른 기능조차 모르겠다.중복을 병합하는 방법이 Lodash에 있습니까?

는 다음과 같은 고려 : 당신은 우리가 객체의 배열을 볼 수 있듯이

enter image description here

는 각 개체가 키 (날짜) 및 배열의 ​​값을 갖는다.

우리가 볼 수 있듯이 같은 날짜의 두 개체가 있습니다. 이 두 객체를 병합하여 하나의 객체 배열로 된 두 객체 대신 두 객체의 배열로 하나의 날짜 (8 월 10 일)를 가질 수 있습니다.

나는 이것이 배열 방법 (필터와 같은 것일 것)이라고 생각한다.

잘 모르겠습니다. 도움이 필요하십니까?

답변

1
//Solution starts here: 

var results = []; 
var temps = {}; 

//Iterate through the dates to find uniq keys(date). 
_.each(dates, function(date) { 
    //Store uniq keys(date) and it's value. 
    _.each(date, function(value, key) { 
    if (temps.hasOwnProperty(key)) { 
     temps[key] = temps[key].concat(value); 
    } else { 
     temps[key] = value; 
    } 
    }); 
}); 

//Tranform the object into an array. 
_.map(temps, function(value, key) { 
    var item = {}; 
    item[key] = value; 
    results.push(item); 
}); 
//results is your answer 
관련 문제