如何向 JavaScript 中的对象数组添加具有索引值的新 属性?
How to add a new property with index value to an object array in JavaScript?
我是论坛的新手,也是 JavaScript 的初学者。请查看下面的代码,我在输出方面遇到了问题 -
let myColor = ["Red", "Green", "White", "Black"];
console.log(myColor.toString());
console.log(myColor.join("+"));
let strings = ["avengers", "captain america", "ironman", "black panther"];
const newStrings = strings.map(elements => {
return elements.toUpperCase()}
);
console.log(newStrings);
const heroes = [
{name: "Spider-Man"},
{name: "Thor"},
{name:"Black Panther"},
{name: "Captain Marvel"},
{name: "Silver Surfer"}
];
heroes.forEach(function(o) {
o.hero = o.name;
delete o.name;
});
let heroesName = [];
for(var i=0; i<heroes.length; i++) {
heroesName.push(heroes[i]); heroesName[i].id=i;
};
console.log(heroesName);
const inputWords = ["spray", "limit", "elite", "exuberant", "destruction", "present"];
console.log(inputWords.splice(3,5));
使用上述代码,我得到了下面提到的输出 -
Red,Green,White,Black
Red+Green+White+Black
[ 'AVENGERS', 'CAPTAIN AMERICA', 'IRONMAN', 'BLACK PANTHER' ]
[
{ hero: 'Spider-Man', id: 0 },
{ hero: 'Thor', id: 1 },
{ hero: 'Black Panther', id: 2 },
{ hero: 'Captain Marvel', id: 3 },
{ hero: 'Silver Surfer', id: 4 }
]
[ 'exuberant', 'destruction', 'present' ]
但是,我试图得到如下输出 -
Red,Green,White,Black
Red+Green+White+Black
[ 'AVENGERS', 'CAPTAIN AMERICA', 'IRONMAN', 'BLACK PANTHER' ]
[
{ id: 0, hero: 'Spider-Man' },
{ id: 1, hero: 'Thor' },
{ id: 2, hero: 'Black Panther' },
{ id: 3, hero: 'Captain Marvel' },
{ id: 4, hero: 'Silver Surfer' }
]
[ 'exuberant', 'destruction', 'present' ]
我这里哪里做错了?所以,我只想要名为 heroes 的对象数组中的 ID 属性 位于英雄名称之前而不是之后。有人可以指导我吗?
属性的顺序无关紧要。如果您想更改顺序,可以在 for 循环中更改一行
let heroesName = [];
for(let i=0; i<heroes.length; i++) {
heroesName.push({ id: i, hero: heroes[i].hero });
};
console.log(heroesName);
我是论坛的新手,也是 JavaScript 的初学者。请查看下面的代码,我在输出方面遇到了问题 -
let myColor = ["Red", "Green", "White", "Black"];
console.log(myColor.toString());
console.log(myColor.join("+"));
let strings = ["avengers", "captain america", "ironman", "black panther"];
const newStrings = strings.map(elements => {
return elements.toUpperCase()}
);
console.log(newStrings);
const heroes = [
{name: "Spider-Man"},
{name: "Thor"},
{name:"Black Panther"},
{name: "Captain Marvel"},
{name: "Silver Surfer"}
];
heroes.forEach(function(o) {
o.hero = o.name;
delete o.name;
});
let heroesName = [];
for(var i=0; i<heroes.length; i++) {
heroesName.push(heroes[i]); heroesName[i].id=i;
};
console.log(heroesName);
const inputWords = ["spray", "limit", "elite", "exuberant", "destruction", "present"];
console.log(inputWords.splice(3,5));
使用上述代码,我得到了下面提到的输出 -
Red,Green,White,Black
Red+Green+White+Black
[ 'AVENGERS', 'CAPTAIN AMERICA', 'IRONMAN', 'BLACK PANTHER' ]
[
{ hero: 'Spider-Man', id: 0 },
{ hero: 'Thor', id: 1 },
{ hero: 'Black Panther', id: 2 },
{ hero: 'Captain Marvel', id: 3 },
{ hero: 'Silver Surfer', id: 4 }
]
[ 'exuberant', 'destruction', 'present' ]
但是,我试图得到如下输出 -
Red,Green,White,Black
Red+Green+White+Black
[ 'AVENGERS', 'CAPTAIN AMERICA', 'IRONMAN', 'BLACK PANTHER' ]
[
{ id: 0, hero: 'Spider-Man' },
{ id: 1, hero: 'Thor' },
{ id: 2, hero: 'Black Panther' },
{ id: 3, hero: 'Captain Marvel' },
{ id: 4, hero: 'Silver Surfer' }
]
[ 'exuberant', 'destruction', 'present' ]
我这里哪里做错了?所以,我只想要名为 heroes 的对象数组中的 ID 属性 位于英雄名称之前而不是之后。有人可以指导我吗?
属性的顺序无关紧要。如果您想更改顺序,可以在 for 循环中更改一行
let heroesName = [];
for(let i=0; i<heroes.length; i++) {
heroesName.push({ id: i, hero: heroes[i].hero });
};
console.log(heroesName);