Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge multiple array of object by ID in javascript?

I want to merge 4 array of object into one array

For example: 4 arrays like

var arr1 =[
  { memberID : "81fs", RatingCW:4.5},
  { memberID : "80fs", RatingCW:4},
  { memberID : "82fs", RatingCW:5 },
  { memberID : "83fs", RatingCW:3},
  { memberID : "84fs", RatingCW:4.7}
];
var arr2 =[
  { memberID : "80fs", ratingWW: 4},
  { memberID : "81fs", ratingWW: 4.5},
  { memberID : "83fs", ratingWW: 3},
  { memberID : "82fs", ratingWW: 5},
  { memberID : "84fs", ratingWW: 3.5}
];

var arr3 =  [
  { memberID : "80fs", incoCW:4},
  { memberID : "81fs", incoCW:4.5},
  { memberID : "82fs", incoCW:5},
  { memberID : "83fs", incoCW:3},
  { memberID : "84fs", incoCW:4.5}
  ];
var arr4 =  [
  { memberID : "80fs", incoWW:3},
  { memberID : "81fs", incoWW:2.5 },
  { memberID : "82fs", incoWW:5 },
  { memberID : "83fs", incoWW:3 },
  { memberID : "84fs", incoWW:6.5 }
];

and expected array like:

var finalArr = [
    { memberID : "80fs", RatingCW:4,ratingWW: 4, incoCW:4, incoWW:3},
    { memberID : "81fs", RatingCW:4.5,ratingWW: 4.5, incoCW:4.5, incoWW:2.5 },
    { memberID : "82fs", RatingCW:5,ratingWW: 5, incoCW:5, incoWW:5 },
    { memberID : "83fs", RatingCW:3,ratingWW: 3, incoCW:3, incoWW:3 },
    { memberID : "84fs", RatingCW:4.7,ratingWW: 3.5, incoCW:4.5, incoWW:6.5 }
  ];

What is the best way to merge using lodash or normal javascript?

like image 217
Shaishab Roy Avatar asked Jul 21 '16 13:07

Shaishab Roy


People also ask

How do I combine multiple objects into one JavaScript?

To merge two objects in JavaScript, you can use the spread ... operator. The spread operator creates a new object with all the properties from the first and second object. If there's two properties with the same name, the property from the second object wins out.

How do I combine arrays of objects into one?

To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.

How do you combine two or more arrays in JavaScript?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays.


1 Answers

With lodash, a lot more readable I think.

var arr1 = [{"memberID":"81fs","RatingCW":4.5},{"memberID":"80fs","RatingCW":4},{"memberID":"82fs","RatingCW":5},{"memberID":"83fs","RatingCW":3},{"memberID":"84fs","RatingCW":4.7}],
    arr2 = [{"memberID":"80fs","ratingWW":4},{"memberID":"81fs","ratingWW":4.5},{"memberID":"83fs","ratingWW":3},{"memberID":"82fs","ratingWW":5},{"memberID":"84fs","ratingWW":3.5}],
    arr3 = [{"memberID":"80fs","incoCW":4},{"memberID":"81fs","incoCW":4.5},{"memberID":"82fs","incoCW":5},{"memberID":"83fs","incoCW":3},{"memberID":"84fs","incoCW":4.5}],
    arr4 = [{"memberID":"80fs","incoWW":3},{"memberID":"81fs","incoWW":2.5},{"memberID":"82fs","incoWW":5},{"memberID":"83fs","incoWW":3},{"memberID":"84fs","incoWW":6.5}];

var merged = _(arr1)
  .concat(arr2, arr3, arr4)
  .groupBy("memberID")
  .map(_.spread(_.merge))
  .value();

console.log(merged);
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>

Here is the codepen: http://codepen.io/kuhnroyal/pen/Wxzdmw

like image 100
kuhnroyal Avatar answered Oct 20 '22 19:10

kuhnroyal