如何做这个数组的组装

let fruitarr = {

{fruitId:201,channel:11},
{fruitId:202,channel:2},
{fruitId:203,channel:13},
{fruitId:201,channel:4},
{fruitId:202,channel:4},

}

fruitparams:{

201:'11,4',
202:'2,4',
203:'13'

}

如何根据水果的id,做统计js拼装

回答:

假如 fruitarrArray的话

let fruitparams = {}
fruitarr.forEach(item => {
    if(fruitparams[item.fruitId]){
       fruitparams[item.fruitId] += `,${item.channel}`
    }else{
       fruitparams[item.fruitId] = `${item.channel}` 
    }

})

回答:

let result = fruitarr.reduce((res, item) => {
  let { fruitId: id, channel: ch } = item
  res[id] = res[id] ? `${res[id]},${ch}` : `${ch}`
  return res
}, {})

回答:

用计算属性?

暂无评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注