How do you go from this:
var array = [{key: [3]}, {key1: [3]}, {key1: [3]}]
var object = {key1: [3], key2: [3]};
to this:
{key: [3], key1: [9], key2: [3]}
All "key" are a userIds like "LQVjUacPgK" as seen in the obj example below.
[N] = is an array of N objects each of which has about 10 key value pairs within.
N = {obj, obj, obj};
obj = {_account: "JDQEPxoy3ktZRP9VEzAMtXLa7rXXedhQ4bARq"
_id: "oQER3vznDwikxm1wdLzJFdVjKL6XomcORMxDL"
amount: 170
category: Array[2]
category_id: "21003000"
date: "2015-06-09"Object
type: Object
userId: "LQVjUacPgK"}
Right now I'm doing this:
var test = _.reduce(_.flatten(array.concat([object])),function(a,b){
return _.extend(a, b);
});
}
};
and getting this result instead:
console.log(test)//{key: [3], key1: [3], key2: [3]}
To be clear, the issue is that key1 has different values between all of the objects. I'd like to keep the values from both so that key1: [9].
This is not an underscore answer, but basically I wouldn't use a reduce operation for this and instead do a simple for-each:
var array = [{key: [3]}, {key1: [3]}, {key1: [3]}]
var object = {key1: [3], key2: [3]};
array.forEach(function(current) {
Object.keys(current).forEach(function(name) {
// object[name] = [((object[name] || [])[0] || 0) + current[name][0]];
object[name] = (object[name] || []).concat(current[name]);
});
});
console.log(JSON.stringify(object)); // {"key1":[3,3,3],"key2":[3],"key":[3]}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다