如何使用.forEach()方法对对象进行循环,并将结果存储在一个新数组中

时间:2023-01-04 21:28:01

I want to loop throught the object, dogNames, and return only the names of dogs I own into the array, myDogs. I want myDogs to appear as such when logged:

我想遍历对象,狗的名字,只返回我自己的狗的名字,我的狗。我想让我的狗在登录时出现:

['Remington','Ruby','Chief','Link']

(“雷明顿”、“红宝石”,“首席”,“链接”)

Most likely, I'm not understanding how the .forEach() method works and/or how to manipulate objects. This is some homework help an I'm just not understanding the .forEach() method after a few hours on this. Thank you for any help. Please just point me in the right direction, I don't want other people to solve this.

很可能,我不了解. foreach()方法是如何工作的,以及/或如何操作对象。这是一些家庭作业,我只是不理解。foreach()方法几小时之后。谢谢你的帮助。请给我指出正确的方向,我不想让其他人来解决这个问题。

First, here is the array I've written.

首先,这是我写的数组。

var dogNames = [
  {name: Kevin,     mine: false},
  {name: Remington, mine: true},
  {name: Bingo,     mine: false},
  {name: Ruger,     mine: false},
  {name: Ruby,      mine: true},
  {name: Gino,      mine: false},
  {name: Chief,     mine: true},
  {name: Watson,    mine: false},
  {name: Link,      mine: true}
];

This is the array I want to store the result to.

这是我要存储结果的数组。

var myDogs = [];

This is what I've attempted to do after some google.

这是我在谷歌之后尝试做的事情。

dogNames.forEach(function(mine){
    if(mine === true){
        myDogs.push(dogNames.name);
    }
});

8 个解决方案

#1


3  

When you iterate through dogNames the argument in function is each object. So what you think is mine is actually the entire object.

在遍历dogNames时,函数中的参数是每个对象。所以你认为是我的实际上是整个物体。

Instead:

而不是:

dogNames.forEach(dogName => {
    if(dogName.mine === true){
        myDogs.push(dogName.name);
    }
});

#2


2  

You could filter and the array and map the names of the filtered array.

您可以过滤和数组,并映射筛选过的数组的名称。

var dogNames = [{ name: 'Kevin', mine: false }, { name: 'Remington', mine: true }, { name: 'Bingo', mine: false }, { name: 'Ruger', mine: false }, { name: 'Ruby', mine: true }, { name: 'Gino', mine: false }, { name: 'Chief', mine: true }, { name: 'Watson', mine: false }, { name: 'Link', mine: true }],
    myDogs = dogNames
        .filter(({ mine }) => mine)
        .map(({ name }) => name)
     
console.log(myDogs);

#3


1  

With function reduce.

与功能降低。

var dogNames = [  {name: 'Kevin',     mine: false},  {name: 'Remington', mine: true},  {name: 'Bingo',     mine: false},  {name: 'Ruger',     mine: false},  {name: 'Ruby',      mine: true},  {name: 'Gino',      mine: false},  {name: 'Chief',     mine: true},  {name: 'Watson',    mine: false},  {name: 'Link',      mine: true}];

var myDogs = dogNames.reduce((a, d) => {
  if (d.mine) a.push(d.name);
  return a;
}, []);
console.log(myDogs);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#4


0  

var myDogs = dogNames.filter(x => x.mine).map(x => x.name);

#5


0  

var dogNames = [
  {name: 'Kevin',     mine: false},
  {name: 'Remington', mine: true},
  {name: 'Bingo',     mine: false},
  {name: 'Ruger',     mine: false},
  {name: 'Ruby',      mine: true},
  {name: 'Gino',      mine: false},
  {name: 'Chief',     mine: true},
  {name: 'Watson',    mine: false},
  {name: 'Link',      mine: true}
];

var myDogs = [];

dogNames.forEach(function(dog){
    if(dog.mine === true){
        myDogs.push(dog.name);
    }
});

console.log(myDogs);

#6


0  

In your approach:

在你的方法:

dogNames.forEach(function(mine){
    if(mine === true){
        myDogs.push(dogNames.name);
    }
});

mine is basically a single element of the array so to access the property of that element you will need dot notation. You can use mine.mine to access the mine property and mine.name to access the name. Now just do the correct comparison and push the correct value in the array.

我的基本是数组的一个元素,所以要访问这个元素的属性,你需要点表示法。你可以用我的。使用mine访问mine属性和mine.name访问name。现在只需做正确的比较,并在数组中按正确的值。

Though, you dont need to use .forEach(), easier approach is to use .filter() and .map() to return the value of name key from the array for mine true.

但是,您不需要使用. foreach(),更简单的方法是使用.filter()和.map(),以从数组中返回name键的值。

dogNames.filter(el => el.mine).map( el => el.name); 

See the full code below.

参见下面的完整代码。

var dogNames = [
  {name: "Kevin",     mine: false},
  {name: "Remington", mine: true},
  {name: "Bingo",     mine: false},
  {name: "Ruger",     mine: false},
  {name: "Ruby",      mine: true},
  {name: "Gino",      mine: false},
  {name: "Chief",     mine: true},
  {name: "Watson",    mine: false},
  {name: "Link",      mine: true}
];

var myDogs = dogNames.filter(el => el.mine).map( el => el.name);
console.log(myDogs)

#7


0  

In your logic, when you do the push, you need to pass mine.name as parameter, instead of dogNames.name

在您的逻辑中,当您执行push时,您需要将mine.name作为参数,而不是dogname .name。

And in your forEach when you put the mine, you're receiving the object like { name: 'Kevin', mine: false }, so, you need specify the propertie you'll check, like in your if statement.

在你的forEach中,当你放入这个矿时,你收到的对象就像{name: 'Kevin',我的:false},所以,你需要指定你要检查的propertie,就像在if语句中一样。

var dogNames = [
  {name: 'Kevin',     mine: false},
  {name: 'Remington', mine: true},
  {name: 'Bingo',     mine: false},
  {name: 'Ruger',     mine: false},
  {name: 'Ruby',      mine: true},
  {name: 'Gino',      mine: false},
  {name: 'Chief',     mine: true},
  {name: 'Watson',    mine: false},
  {name: 'Link',      mine: true}
];

var myDogs = [];

dogNames.forEach(mine => (mine.mine && myDogs.push(mine.name)));

#8


0  

Instead of solving it, let's talk about 'forEach'

而不是解决它,我们来讨论forEach

The argument to Array.prototype.forEach is a function. This function is called for each item in the array.

Array.prototype的参数。forEach是一个函数。为数组中的每个项调用此函数。

That function takes an argument representing a single item in the array. So if I have:

该函数接受一个表示数组中单个项的参数。所以如果我有:

var results = [];
var myArray = [1, 2, 3, 4, 5];

and I have a function

我有一个函数

function addOne(n) {
    results.push(n + 1)
}

I can call forEach on myArray like so

我可以像这样调用myArray中的forEach

myArray.forEach(addOne)

And that will make my results array look like

这会使结果数组看起来像这样

>>> [2, 3, 4, 5, 6]

I think your confusion comes in because you're working with an array of objects, and you tried to iterate on a property of the object (mine: boolean) rather than the object itself ({name: string, mine:boolean})

我认为您的混淆是因为您使用了一个对象数组,并且尝试遍历对象的属性(mine: boolean)而不是对象本身({name: string, mine:boolean})

In your case, you could have an array like so:

在你的例子中,你可以有一个这样的数组:

var secondResults = []
var myObjArray = [{a: 1}, {a: 2}, {a:3}]

and another function

和另一个函数

function addOneToObj(obj) {
   obj.a = obj.a + 1;
   secondResults.push(obj)
}

you could call forEach on it

你可以在上面打个电话。

 myObjArray.forEach(addOneToObj)

and secondResults will look like

第二部分看起来像

>>> [{a: 2}, {a: 3}, {a: 4}]

var results = [];
var myArray = [1, 2, 3, 4, 5];
function addOne(n) {
  results.push(n + 1)
}

myArray.forEach(addOne)
console.log(results); //[2, 3, 4, 5, 6]

var secondResults = []
var myObjArray = [{a: 1}, {a: 2}, {a:3}]
  
function addOneToObj(obj) {
   obj.a = obj.a + 1;
   secondResults.push(obj)
}

myObjArray.forEach(addOneToObj)

console.log(secondResults); //[{a: 2}, {a: 3}, {a: 4}

 

Some notes about why this is a bad approach:

一些关于为什么这是一个糟糕的方法的注释:

Array.prototype.forEach is generally used for for side effects. Array.prototype.forEach doesn't actually return anything. If you're expecting an end result where you've changed the items in the array in some way, you should use .map instead.

Array.prototype。通常用于治疗副作用。Array.prototype。forEach实际上不返回任何东西。如果您期望的结果是您以某种方式更改了数组中的项,那么应该使用.map。

That would simplify our prior example to:

这将简化我们先前的例子:

    
    var myArray = [1, 2, 3, 4, 5];
    function addOne(n) {
      return n + 1
    }

    var results = myArray.map(addOne)
    console.log(results); //[2, 3, 4, 5, 6]

    
    var myObjArray = [{a: 1}, {a: 2}, {a:3}]
      
    function addOneToObj(obj) {
       obj.a = obj.a + 1;
       return (obj)
    }

    var secondResults = myObjArray.map(addOneToObj)

    console.log(secondResults); //[{a: 2}, {a: 3}, {a: 4}]

#1


3  

When you iterate through dogNames the argument in function is each object. So what you think is mine is actually the entire object.

在遍历dogNames时,函数中的参数是每个对象。所以你认为是我的实际上是整个物体。

Instead:

而不是:

dogNames.forEach(dogName => {
    if(dogName.mine === true){
        myDogs.push(dogName.name);
    }
});

#2


2  

You could filter and the array and map the names of the filtered array.

您可以过滤和数组,并映射筛选过的数组的名称。

var dogNames = [{ name: 'Kevin', mine: false }, { name: 'Remington', mine: true }, { name: 'Bingo', mine: false }, { name: 'Ruger', mine: false }, { name: 'Ruby', mine: true }, { name: 'Gino', mine: false }, { name: 'Chief', mine: true }, { name: 'Watson', mine: false }, { name: 'Link', mine: true }],
    myDogs = dogNames
        .filter(({ mine }) => mine)
        .map(({ name }) => name)
     
console.log(myDogs);

#3


1  

With function reduce.

与功能降低。

var dogNames = [  {name: 'Kevin',     mine: false},  {name: 'Remington', mine: true},  {name: 'Bingo',     mine: false},  {name: 'Ruger',     mine: false},  {name: 'Ruby',      mine: true},  {name: 'Gino',      mine: false},  {name: 'Chief',     mine: true},  {name: 'Watson',    mine: false},  {name: 'Link',      mine: true}];

var myDogs = dogNames.reduce((a, d) => {
  if (d.mine) a.push(d.name);
  return a;
}, []);
console.log(myDogs);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#4


0  

var myDogs = dogNames.filter(x => x.mine).map(x => x.name);

#5


0  

var dogNames = [
  {name: 'Kevin',     mine: false},
  {name: 'Remington', mine: true},
  {name: 'Bingo',     mine: false},
  {name: 'Ruger',     mine: false},
  {name: 'Ruby',      mine: true},
  {name: 'Gino',      mine: false},
  {name: 'Chief',     mine: true},
  {name: 'Watson',    mine: false},
  {name: 'Link',      mine: true}
];

var myDogs = [];

dogNames.forEach(function(dog){
    if(dog.mine === true){
        myDogs.push(dog.name);
    }
});

console.log(myDogs);

#6


0  

In your approach:

在你的方法:

dogNames.forEach(function(mine){
    if(mine === true){
        myDogs.push(dogNames.name);
    }
});

mine is basically a single element of the array so to access the property of that element you will need dot notation. You can use mine.mine to access the mine property and mine.name to access the name. Now just do the correct comparison and push the correct value in the array.

我的基本是数组的一个元素,所以要访问这个元素的属性,你需要点表示法。你可以用我的。使用mine访问mine属性和mine.name访问name。现在只需做正确的比较,并在数组中按正确的值。

Though, you dont need to use .forEach(), easier approach is to use .filter() and .map() to return the value of name key from the array for mine true.

但是,您不需要使用. foreach(),更简单的方法是使用.filter()和.map(),以从数组中返回name键的值。

dogNames.filter(el => el.mine).map( el => el.name); 

See the full code below.

参见下面的完整代码。

var dogNames = [
  {name: "Kevin",     mine: false},
  {name: "Remington", mine: true},
  {name: "Bingo",     mine: false},
  {name: "Ruger",     mine: false},
  {name: "Ruby",      mine: true},
  {name: "Gino",      mine: false},
  {name: "Chief",     mine: true},
  {name: "Watson",    mine: false},
  {name: "Link",      mine: true}
];

var myDogs = dogNames.filter(el => el.mine).map( el => el.name);
console.log(myDogs)

#7


0  

In your logic, when you do the push, you need to pass mine.name as parameter, instead of dogNames.name

在您的逻辑中,当您执行push时,您需要将mine.name作为参数,而不是dogname .name。

And in your forEach when you put the mine, you're receiving the object like { name: 'Kevin', mine: false }, so, you need specify the propertie you'll check, like in your if statement.

在你的forEach中,当你放入这个矿时,你收到的对象就像{name: 'Kevin',我的:false},所以,你需要指定你要检查的propertie,就像在if语句中一样。

var dogNames = [
  {name: 'Kevin',     mine: false},
  {name: 'Remington', mine: true},
  {name: 'Bingo',     mine: false},
  {name: 'Ruger',     mine: false},
  {name: 'Ruby',      mine: true},
  {name: 'Gino',      mine: false},
  {name: 'Chief',     mine: true},
  {name: 'Watson',    mine: false},
  {name: 'Link',      mine: true}
];

var myDogs = [];

dogNames.forEach(mine => (mine.mine && myDogs.push(mine.name)));

#8


0  

Instead of solving it, let's talk about 'forEach'

而不是解决它,我们来讨论forEach

The argument to Array.prototype.forEach is a function. This function is called for each item in the array.

Array.prototype的参数。forEach是一个函数。为数组中的每个项调用此函数。

That function takes an argument representing a single item in the array. So if I have:

该函数接受一个表示数组中单个项的参数。所以如果我有:

var results = [];
var myArray = [1, 2, 3, 4, 5];

and I have a function

我有一个函数

function addOne(n) {
    results.push(n + 1)
}

I can call forEach on myArray like so

我可以像这样调用myArray中的forEach

myArray.forEach(addOne)

And that will make my results array look like

这会使结果数组看起来像这样

>>> [2, 3, 4, 5, 6]

I think your confusion comes in because you're working with an array of objects, and you tried to iterate on a property of the object (mine: boolean) rather than the object itself ({name: string, mine:boolean})

我认为您的混淆是因为您使用了一个对象数组,并且尝试遍历对象的属性(mine: boolean)而不是对象本身({name: string, mine:boolean})

In your case, you could have an array like so:

在你的例子中,你可以有一个这样的数组:

var secondResults = []
var myObjArray = [{a: 1}, {a: 2}, {a:3}]

and another function

和另一个函数

function addOneToObj(obj) {
   obj.a = obj.a + 1;
   secondResults.push(obj)
}

you could call forEach on it

你可以在上面打个电话。

 myObjArray.forEach(addOneToObj)

and secondResults will look like

第二部分看起来像

>>> [{a: 2}, {a: 3}, {a: 4}]

var results = [];
var myArray = [1, 2, 3, 4, 5];
function addOne(n) {
  results.push(n + 1)
}

myArray.forEach(addOne)
console.log(results); //[2, 3, 4, 5, 6]

var secondResults = []
var myObjArray = [{a: 1}, {a: 2}, {a:3}]
  
function addOneToObj(obj) {
   obj.a = obj.a + 1;
   secondResults.push(obj)
}

myObjArray.forEach(addOneToObj)

console.log(secondResults); //[{a: 2}, {a: 3}, {a: 4}

 

Some notes about why this is a bad approach:

一些关于为什么这是一个糟糕的方法的注释:

Array.prototype.forEach is generally used for for side effects. Array.prototype.forEach doesn't actually return anything. If you're expecting an end result where you've changed the items in the array in some way, you should use .map instead.

Array.prototype。通常用于治疗副作用。Array.prototype。forEach实际上不返回任何东西。如果您期望的结果是您以某种方式更改了数组中的项,那么应该使用.map。

That would simplify our prior example to:

这将简化我们先前的例子:

    
    var myArray = [1, 2, 3, 4, 5];
    function addOne(n) {
      return n + 1
    }

    var results = myArray.map(addOne)
    console.log(results); //[2, 3, 4, 5, 6]

    
    var myObjArray = [{a: 1}, {a: 2}, {a:3}]
      
    function addOneToObj(obj) {
       obj.a = obj.a + 1;
       return (obj)
    }

    var secondResults = myObjArray.map(addOneToObj)

    console.log(secondResults); //[{a: 2}, {a: 3}, {a: 4}]