使用具有所有用户名的forEach创建一个数组

时间:2019-02-09 02:35:46

标签: javascript arrays object

这是一个数组,我想使用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访问数组中对象的正确方法是什么?

3 个答案:

答案 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)

使用mapdestrcturing

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)

如果您想使用代码消除一些错误

  1. 定义变量常量后就无法更改它的值。
  2. const newArray = array.forEach.... forEach不返回值,因此该分配没有用。
  3. 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);