There are three arrays with repeating _id keys, you need to merge them or merge them into one, while there are no duplicates with the _id .

 var arr1 = [ { '_id': '1', 'man': 20 }, { '_id': '2', 'man': 15 } ] var arr2 = [ { '_id': '1', 'woman': 13 }, { '_id': '2', 'woman': 18 } ] var arr3 = [ { '_id': '1', 'animal': 2 }, { '_id': '2', 'animal': 8 } ] 

It is necessary that the result would be

 [ { '_id': '1', 'woman': 13 'man': 20 'animal': 2 }, { '_id': '2', 'woman': 18 'man': 15 'animal': 8 } ] 

How to make the union of arrays effectively in one cycle? Or maybe there is a special lodash method?

    2 answers 2

     var arr1 = [{ _id: '1', man: 20 }, { _id: '2', man: 15 }] , arr2 = [{ _id: '1', woman: 13 }, { _id: '2', woman: 18 }] , arr3 = [{ _id: '1', animal: 2 }, { _id: '2', animal: 8 }]; var id = '_id' , data = {}; [].concat(arr1, arr2, arr3).forEach(function(item) { if (id in item) { var key = item[id]; data[key] = data[key] || {}; Object.keys(item).forEach(function(property) { data[key][property] = item[property]; }); } }); var result = Object.values(data); console.log(result); 

       var arr1 = [{'_id': '1','man': 20}, {'_id': '2','man': 15}]; var arr2 = [{'_id': '1','woman': 13}, {'_id': '2','woman': 18}]; var arr3 = [{'_id': '1','animal': 2}, {'_id': '2','animal': 8}]; var merged = Object.create(null); arr1.concat(arr2, arr3).forEach(function (obj) { merged[obj._id] = Object.assign(merged[obj._id] || {}, obj); }); console.log(merged); // Если нужен именно массив console.log(Object.values(merged));