Home > Back-end >  how to get the specific key data from multidimensional array in react?
how to get the specific key data from multidimensional array in react?

Time:10-28

I am working with an API where I got data in this format:-

[
  0:{
    id: "1"
    name: "ttp"
    platforms:{
       one: "false",
    }
  },

 1:{
    id: "2"
    name: "spt"
    platforms:{
       one: "true",
       two: "true",
    }
  },
},
]

The data is very large it has more than 100 indexes. I want to check if platforms are present in the index. Suppose if any index contains platform one I want to show its id and don't want to show that index that doesn't have platform one. But I am how to do this in React.

Here the code I want to change, Don't want to use the index [0]

if (response.status === 200) {
    console.log(response.data)
    list = response.data[0].platforms;
 }

CodePudding user response:

You can use filter() to filter your array and get just the elements you need based on a condition.

In your case :

if (response.status === 200) {
    console.log(response.data)
    list = response.data;
    const filteredList = list.filter( (e: any) => (e.platforms.one));
    // filteredList contains just the objects that contains the value one
}
  • Related