转换数组对象key

如何将数组中的对象的 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"
// }]

 

发布了85 篇原创文章 · 获赞 37 · 访问量 15万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章