const myArray = ['position zero', 'position one', 'position three', 'position four'];
//我需要将其转换为
const objectArray = [
{
position: 'position zero'
},
{
position: 'position one'
},
{
position: 'position three'
},
{
position: 'position four'
},
];
//必须是我将引用所有
的相同密钥答案 0 :(得分:3)
map
覆盖当前数组并从map函数返回对象
const myArray = ['position zero', 'position one', 'position three', 'position four'];
const res = myArray.map(data => {
return {position: data}
})
console.log(res)

答案 1 :(得分:0)
您可以将array#map
与Shorthand property names
一起使用。
const myArray = ['position zero', 'position one', 'position three', 'position four'],
result = myArray.map(position => ({position}));
console.log(result);

.as-console-wrapper { max-height:100% !important; top:0;}

答案 2 :(得分:0)
您可以在此处使用.map()
:
const result = myArray.map(s => ({position: s}));
<强>演示:强>
const myArray = ['position zero', 'position one', 'position three', 'position four'];
const result = myArray.map(s => ({position: s}));
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
<强>文档:强>
答案 3 :(得分:0)
用户array.map
使用ES6的shorthand object litteral
:
const myArray = ['position zero', 'position one', 'position three', 'position four'];
const res = myArray.map(position => ({position}));
console.log(res);