这是一个数组,我想使用foreach通过forEach创建一个数组,该数组具有所有带有“!”的用户名。到每个用户名
const array = [
{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
我在JS中尝试的方式:
const double=[];
const newArray = array.forEach(array[i].username)=>{
double.push(array[i].username+"!");
});
console.log(double);
我遇到的错误:
未捕获到的SyntaxError:意外令牌。
使用foreach访问数组中对象的正确方法是什么?
答案 0 :(得分:0)
您可以使用forEach
来做到这一点(请注意,请使用var
而不是const
,因为您无法修改const
):
const array = [{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
var double = [];
array.forEach(person => double.push(person.username + "!"));
console.log(double);
但是,使用map
要简单得多:
const array = [{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
var double = array.map(person => person.username + "!");
console.log(double);
答案 1 :(得分:0)
const array = [{username: "john",team: "red",score: 5, items: ["ball", "book", "pen"]},{username: "becky",team:"blue",score: 10,items: ["tape", "backpack", "pen"]},{username: "susy",team: "red",score: 55,items: ["ball", "eraser", "pen"]},{username: "tyson",team: "green",score: 1,items: ["book", "pen"]},];
let op = array.map(({username})=> username + '!')
console.log(op)
如果您想使用代码消除一些错误
const newArray = array.forEach....
forEach
不返回值,因此该分配没有用。array.forEach(array[i].username)=>...
,这是语法错误。您需要使用array.forEach((array[i].username)=>...
或array.forEach(array[i].username => ....
您可以在下面的代码段中看到更改的代码。
const array = [{username: "john",team: "red",score: 5, items: ["ball", "book", "pen"]},{username: "becky",team:"blue",score: 10,items: ["tape", "backpack", "pen"]},{username: "susy",team: "red",score: 55,items: ["ball", "eraser", "pen"]},{username: "tyson",team: "green",score: 1,items: ["book", "pen"]},];
let double=[];
array.forEach(input=>{
double.push(input.username+"!");
});
console.log(double);
答案 2 :(得分:0)
本来你走对了路,但语法有点错误。
const array = [{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
const doubleNew = [];
const doubleThis = array.forEach(person => {
doubleNew.push(person.username + `!`);
})
console.log(doubleNew);