如何将数组中的对象的 key 换成目标 key 呢?首先想到的是通过 循环 遍历,将 value 重新放入新的 key 中。
var arr = [{
"label": "用户标签",
"prop": "userTags",
}, {
"label": "沟通意向",
"prop": "communicationIntention",
}]
var newArr = arr.map(item => ({
name: item.label,
value: item.prop,
...item
}))
console.log(newArr)
输出结果:
// [{
// "name":"用户标签","value":"userTags",
// "label":"用户标签","prop":"userTags"
// },{
// "name":"沟通意向","value":"communicationIntention",
// "label":"沟通意向","prop":"communicationIntention"
// }]