1

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].

4
  • I'm confused about what [N] represents. Could you expand the input sample to be more representative of your actual problem? Commented Jun 11, 2015 at 4:25
  • @Jack I broke open the data structure more for you so you could see N. Commented Jun 11, 2015 at 4:35
  • So, instead of an array with a single number element it's an array of objects? And should the result be an array with a single object element, an array of multiple object elements ... etc Commented Jun 11, 2015 at 4:36
  • The result should be an object with key value pairs where the values are an array of objects. The method that I tried (and am working in parallel to your help on) is returning a result where some of the values are overwritten as underscore tends to do when combining arrays and I need to have key value pairs where values are added to one another when there are redundant keys. Commented Jun 11, 2015 at 4:47

2 Answers 2

2

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]}

Sign up to request clarification or add additional context in comments.

6 Comments

Trying it now. Thanks for the suggestion!
It works when I stringify on my console and in the code snippet but when I make your answer a var and console.log it is undefined. Is this right or am I doing something wrong? I need to pass this object on to something else and it's not clear to me what to do with this.
I just tried to pass it on and it provides the wrong result. I tried it with dummy data as shown it works correctly. Any other suggestions?
@rashadb The problem with your question is that we have no idea what those data structures mean, so we have to make assumptions .. please provide an example in which my answer doesn't work or explain what kind of data is stored in your variables.
I add some detail about key (UserId) and [N] which is itself an array of objects. Let me know what else I might add to help.
|
1

Similar to Jack's answer (also non-underscore), but it creates a new object, it doesn't modify the existing object Object:

var array = [{key: [3]}, {key1: [3]}, {key1: [3]}]
var object = {key1: [3], key2: [3]};

var x = array.concat([object]).reduce(function(prev, curr) {
  Object.keys(curr).forEach(function(key){
    if (prev.hasOwnProperty(key)) {
      prev[key][0] += curr[key][0];
    } else {
      prev[key] = curr[key];
    }
  });
  return prev;
},{});

console.log(JSON.stringify(x));  // {"key":[3],"key1":[9],"key2":[3]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.