如何做这个数组的组装

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拼装

阅读 2.4k
3 个回答
let result = fruitarr.reduce((res, item) => {
  let { fruitId: id, channel: ch } = item
  res[id] = res[id] ? `${res[id]},${ch}` : `${ch}`
  return res
}, {})

假如 fruitarrArray的话

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

})

用计算属性?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏