[英]How i can merge this array of object?
I have this array of objects:我有这个对象数组:
let array1 = [{
id: 1,
code: 'wawa'
},
{
id: 2,
code: 'qwqw'
}]
let array2 = [{
id: 1,
code: 'ewew'
},
{
id: 3,
code: 'eee'
}]
let array3 = [{
id: 3,
code: 'ww'
},
{
id: 1,
code: 'ww'
},
{
id: 4,
code: 'q'
}
]
let arrayGeral = new Array()
arrayGeral.push(array1)
arrayGeral.push(array2)
arrayGeral.push(array3)
How i can merge the objects in the arrayGeral to a one array object?如何将 arrayGeral 中的对象合并为一个数组对象?
I can do what i want using lodash, but i can't think in a way to iterate and make this automatic:我可以使用 lodash 做我想做的事,但我无法以某种方式进行迭代并使此自动化:
let merged = _.merge(_.keyBy(array1, 'id'), _.keyBy(array2, 'id'))
merged = _.merge(_.keyBy(merged, 'id'), _.keyBy(array3, 'id'))
I am assuming you want to merge them to 1 array without grouping by id.我假设您想将它们合并到 1 个数组而不按 id 分组。 If that is the case you can use
concat
to merge them to 1 array like so: mergedArray = array1.concat(array2, array3)
如果是这种情况,您可以使用
concat
将它们合并为 1 个数组,如下所示: mergedArray = array1.concat(array2, array3)
let array1 = [{ id: 1, code: 'wawa' }, { id: 2, code: 'qwqw' }] let array2 = [{ id: 1, code: 'ewew' }, { id: 3, code: 'eee' }] let array3 = [{ id: 3, code: 'ww' }, { id: 1, code: 'ww' }, { id: 4, code: 'q' } ] var concatArr = array1.concat(array2, array3); console.log(concatArr);
I suppose that the last values should be kept.我想应该保留最后的值。 If that's the case then you must inverse the way you push:
如果是这种情况,那么您必须反转您的推送方式:
let arrayGeral = new Array();
arrayGeral.push(...array3);
arrayGeral.push(...array2);
arrayGeral.push(...array1);
Now all you have to do is to use the _.uniqBy
lodash function:现在你要做的就是使用
_.uniqBy
lodash 函数:
_.uniqBy(arrayGeral, 'id');
let array1 = [ { id: 1, code: 'wawa' }, { id: 2, code: 'qwqw' } ]; let array2 = [ { id: 1, code: 'ewew' }, { id: 3, code: 'eee' } ]; let array3 = [ { id: 3, code: 'ww' }, { id: 1, code: 'ww' }, { id: 4, code: 'q' } ]; let arrayGeral = new Array(); arrayGeral.push(...array3); arrayGeral.push(...array2); arrayGeral.push(...array1); console.log(_.uniqBy(arrayGeral, 'id'));
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
If this is your output:如果这是您的输出:
[
{ id: 1, code: [ 'wawa', 'ewew', 'ww' ] },
{ id: 2, code: [ 'qwqw' ] },
{ id: 3, code: [ 'eee', 'ww' ] },
{ id: 4, code: [ 'q' ] }
]
Then refer to the below code:然后参考下面的代码:
let array1 = [{ id: 1, code: 'wawa'}, { id: 2, code: 'qwqw' }] let array2 = [{ id: 1, code: 'ewew'}, { id: 3, code: 'eee' }] let array3 = [{ id: 3, code: 'ww' }, { id: 1, code: 'ww' }, { id: 4, code: 'q' }] var concated = array1.concat(array2).concat(array3); //Flaten the concatenated array let output = concated.reduce((acc, cur) => { let entry = acc.find(item => item.id === cur.id); if (entry && Array.isArray(entry.code)) { //Check if it has sth in the array entry.code.push(cur.code); } else { acc.push({id: cur.id, code: [cur.code]}); //For the 1st time, add it to the array } //console.log(acc); return acc; }, []); console.log(output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.