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

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

Get list of duplicate objects in an array of objects

You can use Array#reduce to make a counter lookup table based on the id key, then use Array#filter to remove any items that appeared only once in the lookup table. Time complexity is O(n). const values = [{id: 10, name: ‘someName1’}, {id: 10, name: ‘someName2′}, {id: 11, name:’someName3’}, {id: 12, name: ‘someName4’}]; const lookup = … Read more

Replacing objects in array

You can use Array#map with Array#find. arr1.map(obj => arr2.find(o => o.id === obj.id) || obj); var arr1 = [{ id: ‘124’, name: ‘qqq’ }, { id: ‘589’, name: ‘www’ }, { id: ’45’, name: ‘eee’ }, { id: ‘567’, name: ‘rrr’ }]; var arr2 = [{ id: ‘124’, name: ‘ttt’ }, { id: ’45’, name: … Read more

Comparing two arrays of objects, and exclude the elements who match values into new array in JS

well, this using lodash or vanilla javascript it depends on the situation. but for just return the array that contains the differences can be achieved by the following, offcourse it was taken from @1983 var result = result1.filter(function (o1) { return !result2.some(function (o2) { return o1.id === o2.id; // return the ones with equal id … Read more

How to use throttle or debounce with React Hook?

After some time passed I’m sure it’s much easier to handle things by your own with setTimeout/clearTimeout(and moving that into separate custom hook) than working with functional helpers. Handling later one creates additional challenges right after we apply that to useCallback that can be recreated because of dependency change but we don’t want to reset … Read more

Lodash remove duplicates from array

_.unique no longer works for the current version of Lodash as version 4.0.0 has this breaking change. The functionality of _.unique was splitted into _.uniq, _.sortedUniq, _.sortedUniqBy, and _.uniqBy. You could use _.uniqBy like this: _.uniqBy(data, function (e) { return e.id; }); …or like this: _.uniqBy(data, ‘id’); Documentation: https://lodash.com/docs#uniqBy For older versions of Lodash (< … Read more

using lodash .groupBy. how to add your own keys for grouped output?

You can do it like this in both Underscore and Lodash (3.x and 4.x). var data = [{ “name”: “jim”, “color”: “blue”, “age”: “22” }, { “name”: “Sam”, “color”: “blue”, “age”: “33” }, { “name”: “eddie”, “color”: “green”, “age”: “77” }]; console.log( _.chain(data) // Group the elements of Array based on `color` property .groupBy(“color”) // … Read more

How to do a deep comparison between 2 objects with lodash?

An easy and elegant solution is to use _.isEqual, which performs a deep comparison: var a = {}; var b = {}; a.prop1 = 2; a.prop2 = { prop3: 2 }; b.prop1 = 2; b.prop2 = { prop3: 3 }; console.log(_.isEqual(a, b)); // returns false if different <script src=”https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js”></script> However, this solution doesn’t show which … Read more