簡體   English   中英

Lodash:組合數組 object

[英]Lodash: Combine array object

例如,我想將具有相同 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.

在此處輸入圖像描述

並且當它結合相同的 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);
這是結果&&我不知道代碼是什么所以我忽略了 在此處輸入圖像描述

使用_.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);

真的不需要 lodash,但既然要求你可以使用_.forEach循環,然后使用_.find()檢查我們是否已經擁有該項目

片段我還包括一個非 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM