数组转对象的方法

const Arr = [
  { label: '男', value: 0 },
  { label: '女', value: 1 },
]
const needObj = {}
Arr.forEach((item) => {
  const key = item.value
  const value = item.label
  needObj[key] = value
})
console.log(needObj) // {0: '男', 1: '女'}
posted @ 2022-05-24 21:19  粥粥。  阅读(683)  评论(0)    收藏  举报