简体   繁体   English

使用 map 迭代数组内的对象

[英]using map to iterate over an object that is inside of an array

Here is my code:这是我的代码:

class SomeClass{
    constructor(props){
        super(props);
           this.state = {
              SomeElement:
              [
                {
                   AAAQQQ: "",
                   AAA: ""
                },
              ],
              errorMessage: ""
           };
       }

    MyFunction = (someParam) =>{
      //do something
    }

}

 

I can access errorMessage by doing:我可以通过执行以下操作访问 errorMessage:

this.state.errorMessage this.state.errorMessage

But for accessing AAAQQQ, do I say:但是对于访问AAAQQQ,我说:

this.state.SomeElement.map((item, index) =>{
    SomeFunction(item.AAAQQQ);
});

My Question :我的问题

Is the above approach correct to access an element inside of an array of objects ?上述方法是否正确访问对象数组中的元素?

The item.AAAQQQ part is. item.AAAQQQ部分是。 The map part isn't if you aren't using the array map creates.如果您不使用数组map创建,则map部分不是。 I've written up why here and what to do instead, but basically use for-of :我已经写了为什么在这里以及该怎么做,但基本上使用for-of

for (const item of this.state.SomeElement) {
    SomeFunction(item.AAAQQQ);
}

or forEach :forEach

this.state.SomeElement.forEach(item => {
    SomeFunction(item.AAAQQQ);
});

forEach is handy when you need the index as well as the item ( .forEach((item, index) => { /*...*/ }) ).当您需要索引以及项目( .forEach((item, index) => { /*...*/ }) )时, forEach非常方便。

I will list down various ways to access the elements.我将列出访问元素的各种方法。 Before that, I would like to tell you when can you use which javascript functions.在此之前,我想告诉您什么时候可以使用哪些javascript函数。

Array Functions like map and filter will return an array as output.像 map 和 filter 这样的数组函数将返回一个数组作为输出。 So, if you have a case wherein you want to perform some operation on each array element and store the final result in an array then we can use the map and filter method.因此,如果您想对每个数组元素执行一些操作并将最终结果存储在数组中,那么我们可以使用 map 和 filter 方法。

You can use a map when you want to save the result for all the elements in the array and you can use a filter if you want to save only for some elements.当您想保存数组中所有元素的结果时可以使用映射,如果只想保存某些元素,则可以使用过滤器。

A simple example is here一个简单的例子在这里

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const allSquare = arr.map(element => (element * element));
const evenElements = arr.filter(element => {
  if(arr%2 == 0) {
     return true;  
  }
  return false;
});

// more concise way to write
const evenElements = arr.filter(element => !(element%2));

Basically in filter whenever you return true, the element will be saved otherwise skipped.基本上在过滤器中,每当您返回 true 时,元素将被保存,否则将被跳过。

So, the point to note down here is whenever you want another array as output then you can use a map and filter.因此,这里要注意的一点是,只要您想要另一个数组作为输出,就可以使用映射和过滤器。 Otherwise, you have other iterative ways like for, forEach.否则,您还有其他迭代方式,例如 for、forEach。

for is basically the normal for loop which we use in all programming languages and forEach accepts callback function so you can perform operations there. for基本上是我们在所有编程语言中使用的普通 for 循环, forEach接受回调函数,因此您可以在那里执行操作。

In your case, you don't want to store any output for the someFunctions so you can use forEach, and that is the best way to access the elements.在您的情况下,您不想为 someFunctions 存储任何输出,因此您可以使用 forEach,这是访问元素的最佳方式。

If you have only one object then you can directly use the round bracket index access method .如果您只有一个对象,那么您可以直接使用圆括号索引访问方法。 this.state.someElement[0].AAAQQQ

Otherwise, if you have array of object then forEach is the best approach becuase you don't have to do manual work of writing like this否则,如果您有对象数组,那么 forEach 是最好的方法,因为您不必像这样手动编写

for(let i = 0; i<this.state.someElement.length; i++) {
  // do something
}

you can directly do like this你可以直接这样做

this.state.someElement.forEach((ele, index) => {
  // do something
})

If you have object of object like this如果你有这样的对象

const people = {
  person1: { name: 'John', age: 10 },
  person2: { name: 'Ajay', age: 12 },
  person3: { name: 'Rocky', age: 15 }
}

Then you can access element like this.然后你可以像这样访问元素。

const peopleKeys = Object.keys(people);
const names = peopleKeys.map((key) => {
  return people[key].name;
})
console.log(names) // ['John', 'Ajay', 'Rocky']

Note that here we want output from every element hence we have use map.请注意,这里我们想要每个元素的输出,因此我们使用了 map。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM