Adding the values from an object to an array of objects without overriding existing key values

rashadb

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

Ja͢ck

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

lodash/underscore find objects by key that is in values of array

분류에서Dev

lodash/underscore find objects by key that is in values of array

분류에서Dev

Make a unique list of values from a particular key existing anywhere in a deep array

분류에서Dev

Concat Object Values from Key with Comma

분류에서Dev

adding values on array java

분류에서Dev

Add new array values to existing array position

분류에서Dev

Xcode , Parse - Storing values from an array object in a local array

분류에서Dev

How to remove duplicate values from a multidimentional array according to a key

분류에서Dev

Giving int values to key in an array

분류에서Dev

replace key with new array values

분류에서Dev

Ruby on Rails - Get array of values from array of hash with particular order of existing keys

분류에서Dev

array_combine with key has only one from array 1 and multiple values from array 2

분류에서Dev

Want to retrieve values not other details from array object in rally

분류에서Dev

PHP Accessing Values quickly an Arrray holding objects which one element of the object has an array

분류에서Dev

How do I sort an array filled with objects, based on two values of the object?

분류에서Dev

Returning values from an Object in Javascript

분류에서Dev

Overriding object getter without defineProperty

분류에서Dev

Reduce Array to objects that share certain property: values

분류에서Dev

How to get all values of objects inside array

분류에서Dev

Word frequency for array of key/values on javascript

분류에서Dev

PHP How to sum values of the array of the same key

분류에서Dev

Compare array values with same key in PHP

분류에서Dev

Extract "N" Values from an Array

분류에서Dev

Getting all keys & their values from nested objects in JSON array (w/out jQuery)

분류에서Dev

transform JSON array of objects, create new property from two property values

분류에서Dev

StandardSQL BigQuery values from key:value pairs within an Array into separate columns. How?

분류에서Dev

Accessing a PHP array object(stdClass) values

분류에서Dev

JS concatenating object property values (numeric) instead of adding

분류에서Dev

print all the values of a group of objects without iterating through it

Related 관련 기사

  1. 1

    lodash/underscore find objects by key that is in values of array

  2. 2

    lodash/underscore find objects by key that is in values of array

  3. 3

    Make a unique list of values from a particular key existing anywhere in a deep array

  4. 4

    Concat Object Values from Key with Comma

  5. 5

    adding values on array java

  6. 6

    Add new array values to existing array position

  7. 7

    Xcode , Parse - Storing values from an array object in a local array

  8. 8

    How to remove duplicate values from a multidimentional array according to a key

  9. 9

    Giving int values to key in an array

  10. 10

    replace key with new array values

  11. 11

    Ruby on Rails - Get array of values from array of hash with particular order of existing keys

  12. 12

    array_combine with key has only one from array 1 and multiple values from array 2

  13. 13

    Want to retrieve values not other details from array object in rally

  14. 14

    PHP Accessing Values quickly an Arrray holding objects which one element of the object has an array

  15. 15

    How do I sort an array filled with objects, based on two values of the object?

  16. 16

    Returning values from an Object in Javascript

  17. 17

    Overriding object getter without defineProperty

  18. 18

    Reduce Array to objects that share certain property: values

  19. 19

    How to get all values of objects inside array

  20. 20

    Word frequency for array of key/values on javascript

  21. 21

    PHP How to sum values of the array of the same key

  22. 22

    Compare array values with same key in PHP

  23. 23

    Extract "N" Values from an Array

  24. 24

    Getting all keys & their values from nested objects in JSON array (w/out jQuery)

  25. 25

    transform JSON array of objects, create new property from two property values

  26. 26

    StandardSQL BigQuery values from key:value pairs within an Array into separate columns. How?

  27. 27

    Accessing a PHP array object(stdClass) values

  28. 28

    JS concatenating object property values (numeric) instead of adding

  29. 29

    print all the values of a group of objects without iterating through it

뜨겁다태그

보관