最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Lodash - How to merge two arrays of objects based on key? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to concat two arrays of objects with lodash. I have these two array and I use concat operator in this way:

 var array1 = [{ id: 1, name: 'doc1' }, { id: 2, name: 'doc2' }];
 var array2 = [{id:1, name:'doc1'}, {id:3, name:'doc3'}, {id:4, name:'doc4'}];

 var array3 = _.concat(array1, array2);

I would like an array like this

 [{id:1, name:'doc1'}, {id:2, name:'doc2'}, {id:3, name:'doc3'}, {id:4, name:'doc4'}]]

But with concat function I obtain an array like this:

  [{ id: 1, name: 'doc1' }, { id: 1, name: 'doc1' }, { id: 2, name: 'doc2' }, { id: 3, name: 'doc3' }, { id: 4, name: 'doc4' }]]

I want to avoid the repetition of an object with the same id...Which kind of operator can I use?

I'm trying to concat two arrays of objects with lodash. I have these two array and I use concat operator in this way:

 var array1 = [{ id: 1, name: 'doc1' }, { id: 2, name: 'doc2' }];
 var array2 = [{id:1, name:'doc1'}, {id:3, name:'doc3'}, {id:4, name:'doc4'}];

 var array3 = _.concat(array1, array2);

I would like an array like this

 [{id:1, name:'doc1'}, {id:2, name:'doc2'}, {id:3, name:'doc3'}, {id:4, name:'doc4'}]]

But with concat function I obtain an array like this:

  [{ id: 1, name: 'doc1' }, { id: 1, name: 'doc1' }, { id: 2, name: 'doc2' }, { id: 3, name: 'doc3' }, { id: 4, name: 'doc4' }]]

I want to avoid the repetition of an object with the same id...Which kind of operator can I use?

Share Improve this question edited Jun 2, 2022 at 19:54 danronmoon 3,8735 gold badges35 silver badges58 bronze badges asked Oct 28, 2020 at 10:47 Andrew88Andrew88 851 gold badge1 silver badge5 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

You can get the result using merge and keyBy lodash functions.

var array1 = [{id:1, name:'doc1'}, {id:2, name:'doc2'}];
var array2 = [{id:1, name:'doc1'}, {id:3, name:'doc3'}, {id:4, name:'doc4'}];

var merged = _.merge(_.keyBy(array1, 'id'), _.keyBy(array2, 'id'));
var values = _.values(merged);
console.log(values);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

Use _.unionBy() to combine two arrays, and remove duplicates that have the same unique field (id in this case). The lodash's union methods create an array of unique values. The _.unionBy() method accepts an iteratee which is invoked for each element of each array to generate the criterion by which uniqueness is computed.

const array1 = [{id:1, name:'doc1'}, {id:2, name:'doc2'}];
const array2 = [{id:1, name:'doc1'}, {id:3, name:'doc3'}, {id:4, name:'doc4'}];

const result = _.unionBy(array1, array2, 'id');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

You have uniqWith in lodash that allows you to do this: https://lodash.com/docs#uniqWith

var array1 = [{id:1, name:'doc1'}, {id:2, name:'doc2'}];
var array2 = [{id:1, name:'doc1'}, {id:3, name:'doc3'}, {id:4, name:'doc4'}];

var values = _.uniqWith([...array1, ...array2], _.isEqual);
console.log(values);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论