js中使用reduce将json数组 转换为json

Posted 江山一族

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js中使用reduce将json数组 转换为json相关的知识,希望对你有一定的参考价值。

let arr = [{
"code": "badge",
"priceList": [{
"amount": 3000
}]
},

{
"code": "DigitalPhoto",
"priceList": [{
"amount": 1990
}]
}
]

let arr2 = arr.reduce((pre,cur)=>{
pre[cur.code] = cur.priceList
return pre
},{})
console.log(arr2)

打印结果:

{ badge: [ { amount: 3000 } ],
DigitalPhoto: [ { amount: 1990 } ] }

















以上是关于js中使用reduce将json数组 转换为json的主要内容,如果未能解决你的问题,请参考以下文章