[英]Lodash: Combine array object
I want to combine the array with same id for example例如,我想将具有相同 id 的数组组合起来
var student = [{
'id': 'xx001',
'code': 'taller',
'item': 2,
'date': '2019-01-01'
},{
'id': 'xx001',
'code': 'taller',
'item': 5,
'date': '2019-01-01'
},{
'id': 'xx001',
'code': 'taller',
'item': 5,
'date': '2019-03-24'
},{
'id': 'xx002',
'code': 'small',
'item': 2,
'date': '2019-01-01'
}]
and the output should be.
and when it combine the same id it will added the item and if the date is not the same it should seperate.并且当它结合相同的 id 时,它将添加该项目,如果日期不一样,它应该分开。
var student = [{ 'id': 'xx001', 'code': 'taller', 'item': 2 },{ 'id': 'xx001', 'code': 'taller', 'item': 5 },{ 'id': 'xx002', 'code': 'small', 'item': 2 }]; let idList = Array.from(new Set(student.map(p => p.id))); console.log(idList); let result = []; idList.filter(p => { let filteredList = student.filter(k => p == k.id); console.log(filteredList); let itemList = filteredList.map(j => j.item); let sumItem = itemList.reduce((a, b) => a + b, 0); result.push({'id': p, 'code': filteredList[0].code, 'item': sumItem}); }) console.log(result);
Using _.reduce()
, https://codepen.io/1010543618/pen/VwwgVwm?editors=0010使用
_.reduce()
, https://codepen.io/1010543618/pen/VwwgVwm?editors=0010
var student = [{
'id': 'xx001',
'code': 'taller',
'item': 2
},{
'id': 'xx001',
'code': 'taller',
'item': 5
},{
'id': 'xx002',
'code': 'small',
'item': 2
}];
var result = _.reduce(student, function(res, stu, index, arr){
var fstu = _.find(res, function(d){
return d.id === stu.id;
});
if(fstu){
fstu.item += stu.item;
}else{
res.push(_.clone(stu));
}
return res;
}, []);
console.log(result);
Don't really need lodash, but since it's asked for you can loop using _.forEach
and then check if we already have the item using _.find()
真的不需要 lodash,但既然要求你可以使用
_.forEach
循环,然后使用_.find()
检查我们是否已经拥有该项目
Snippet I have also included a non-lodash version:片段我还包括一个非 lodash 版本:
var students = [{ 'id': 'xx001', 'code': 'taller', 'item': 2 }, { 'id': 'xx001', 'code': 'taller', 'item': 5 }, { 'id': 'xx002', 'code': 'small', 'item': 2 }]; const output = []; _.each(students, x => { const existing = _.find(output, { id: x.id }); if (existing) { existing.item += x.item; } else { output.push(x); } }); console.info(output); const students2 = [{ 'id': 'xx001', 'code': 'taller', 'item': 2 }, { 'id': 'xx001', 'code': 'taller', 'item': 5 }, { 'id': 'xx002', 'code': 'small', 'item': 2 }]; const output2 = []; students2.forEach(x => { const existing = output2.find(y => y.id === x.id); if (existing) { existing.item += x.item; } else { output2.push(x); } }); console.log(output2); var students3 = [{ 'id': 'xx001', 'code': 'taller', 'item': 2, 'date': '2019-01-01' },{ 'id': 'xx001', 'code': 'taller', 'item': 5, 'date': '2019-01-01' },{ 'id': 'xx001', 'code': 'taller', 'item': 5, 'date': '2019-03-24' },{ 'id': 'xx002', 'code': 'small', 'item': 2, 'date': '2019-01-01' }] const output3 = []; students3.forEach(x => { const existing = output3.find(y => y.id === x.id && y.date === x.date); if (existing) { existing.item += x.item; } else { output3.push(x); } }); console.log(output3);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash-compat/3.10.2/lodash.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.