Push variables as object names into the array js

Push variables as object names into the array js

let a = [
{name:'张胜男1',age:12},
{name:'张胜男2',age:14}
]

// 我想要的结果是[{张胜男1:12},{张胜男2:14},]

let arr = []
a.forEach(item => {
arr.push({[item.name]:item.age})
})
console.log(arr) // 就得到想要的的结果了

 

Guess you like

Origin blog.csdn.net/qq706352062/article/details/109772031