我想返回一个新数组,但将属性值作为属性名称。
const temp = [
{name: 'james'},
{name: 'ally'}
]
const new = temp.map(obj => ({
`${obj.name}`: null
}))
显然这样不行。有什么线索吗? https://jsfiddle.net/qg8ofom1/
最佳答案
const temp = [
{name: 'james'},
{name: 'ally'}
];
const newObj = temp.map(obj => ({
[obj.name]: null
}));
console.log(newObj);
关于javascript - 使用映射使用属性值作为属性键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45748455/