How to merge two dictionaries in javascript

Manuel Schiller

I have two arrays of objects:

Array1:

var myArr1 = [];
myArr1["1"]={any:1,some:1};
myArr1["2"]={any:2,some:2};
myArr1["3"]={any:3,some:3};

Array2:

var myArr2 = [];
myArr2["1"]={other:1};    
myArr2["2"]={other:2};

And I want them to be merged by their keys into a new Attribute, so the result will be:

[
  {any:1,some:1,myNewAttribute:{other:1}},
  {any:2,some:2,myNewAttribute:{other:2}},
  {any:3,some:3,myNewAttribute:{other:3}}
]

I tried to achieve it with lodash's _.merge() but I failed miserably. _.merge only adds the second array after the first, but does not match their keys / ids.

Nina Scholz

You could map the second array to a new property and merge later.

With lodash

var data1 = [{ any: 1, some: 1 }, { any: 2, some: 2 }, { any: 3, some: 3 }],
    data2 = [{ other: 1 }, { other: 2 }, { other: 3 }];

console.log(_.merge(data1, _.map(data2, x => ({ myNewAttribute: x }))));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

With ES6, without lodash

var data1 = [{ any: 1, some: 1 }, { any: 2, some: 2 }, { any: 3, some: 3 }],
    data2 = [{ other: 1 }, { other: 2 }, { other: 3 }];

console.log(data1.map((a, i) => Object.assign({}, a, { myNewAttribute: data2[i] })));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to merge two dictionaries with same key names

From Dev

merge two dictionaries by key

From Dev

merge two dictionaries by key

From Dev

Merge two dictionaries

From Dev

How can I merge two nested dictionaries together?

From Dev

How to use Lambda to merge two Dictionary with List<string> contain in Dictionaries

From Dev

How to merge two Dictionaries including only unique values

From Dev

How can I merge two spell checker dictionaries?

From Dev

How can I merge two dictionaries with multiple key value pairs

From Dev

How do I merge two dictionaries in C#

From Dev

Generic function to merge two dictionaries

From Dev

To merge two dictionaries of list in Python

From Dev

How to merge multiple dictionaries

From Java

How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?

From Dev

How do I merge two dictionaries in a single expression (taking union of dictionaries)?

From Dev

How do I merge two dictionaries in a single expression (taking union of dictionaries)?

From Dev

How to efficiently merge two arrays in Javascript?

From Dev

How to merge two .csv files in javascript?

From Dev

How to merge the two array of objects using javascript

From Dev

How can I Merge two JavaScript objects?

From Dev

How to Merge Two JavaScript arrays using AngularJs

From Dev

Merge two dictionaries in python with lists as values

From Dev

Merge two 2D dictionaries python

From Dev

Merge Two Dictionaries that Share Same Key:Value

From Dev

Merge two dictionaries in python with lists as values

From Dev

How to merge dictionaries within two lists if they have a common key-value pair?

From Dev

Javascript merge two objects

From Dev

how to efficiently merge 3 or more dictionaries

From Dev

Dictionaries: how to compare values and merge if values are the same?

Related Related

  1. 1

    How to merge two dictionaries with same key names

  2. 2

    merge two dictionaries by key

  3. 3

    merge two dictionaries by key

  4. 4

    Merge two dictionaries

  5. 5

    How can I merge two nested dictionaries together?

  6. 6

    How to use Lambda to merge two Dictionary with List<string> contain in Dictionaries

  7. 7

    How to merge two Dictionaries including only unique values

  8. 8

    How can I merge two spell checker dictionaries?

  9. 9

    How can I merge two dictionaries with multiple key value pairs

  10. 10

    How do I merge two dictionaries in C#

  11. 11

    Generic function to merge two dictionaries

  12. 12

    To merge two dictionaries of list in Python

  13. 13

    How to merge multiple dictionaries

  14. 14

    How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?

  15. 15

    How do I merge two dictionaries in a single expression (taking union of dictionaries)?

  16. 16

    How do I merge two dictionaries in a single expression (taking union of dictionaries)?

  17. 17

    How to efficiently merge two arrays in Javascript?

  18. 18

    How to merge two .csv files in javascript?

  19. 19

    How to merge the two array of objects using javascript

  20. 20

    How can I Merge two JavaScript objects?

  21. 21

    How to Merge Two JavaScript arrays using AngularJs

  22. 22

    Merge two dictionaries in python with lists as values

  23. 23

    Merge two 2D dictionaries python

  24. 24

    Merge Two Dictionaries that Share Same Key:Value

  25. 25

    Merge two dictionaries in python with lists as values

  26. 26

    How to merge dictionaries within two lists if they have a common key-value pair?

  27. 27

    Javascript merge two objects

  28. 28

    how to efficiently merge 3 or more dictionaries

  29. 29

    Dictionaries: how to compare values and merge if values are the same?

HotTag

Archive