数组转对象

LiveMerchantList  =
[ 
{408: "222华为商家"},
{409: "222小米商家"}
]
 
 
 
 
  console.log(LiveMerchantList,'1')
 
  console.log(
LiveMerchantList.reduce(
   (prev, item) => ({ ...prev, [item.merchantId]: item.merchantName }),
    {},
  )
,'LiveMerchantList')
 
 console.log(
LiveMerchantList.map(item =>({[item.merchantId]:item.merchantName}))
,'2')
 
 
let obj = {}
LiveMerchantList.forEach(item=>{  obj[item.merchantId] = item.merchantName})
console.log('obj',JSON.stringify(obj,null,2))
posted @ 2021-01-04 13:42  外行的小白  阅读(93)  评论(0)    收藏  举报