How to browse an array of Objects within another Object Array

Asked

Viewed 110 times

-1

Hello, I have two arrays of objects within an array of objects, and I would like to be able to access them and render the objects from within (the objects of the object obj1 and of obj2)

Well, I thought of these arrays as a way to manipulate two variables (obj1 and obj2) with their respective values to render them using a map, however the way I did does not work, I would like to know how to do this the right way and also would like to know if there is an easier and simpler way to organize all these object arrays (good practices of code).

const teste = [
{
  obj1: [
    {
      teste: "teste",
      teste2: "teste2",
    },
  ],
},
{
  obj2: [
    {
      teste: "teste",
      teste2: "teste2",
    },
  ],
},
];

console.log(teste.map((e) => e.map((x) => x.obj1)));

From now on Thank you !

  • But the question is how to go through this structure with for ? or is what specifically ? What are you trying to do with these arrays ?

  • Isac then, I had thought of using a map because the idea is this, I have two components (in React) that will appear together on the screen and both have fixed data (e.g., title, image description) and instead of passing these props to these components when calling the component (ex <Card title="title />) I would put all this information in these arrays and could render it to nois components using a map would look something like this test.map(((e) => { <Card title={e.title} /> }) understood ?

1 answer

2

Good night buddy, that object of yours there is a little confusing, but you will manage to interact for it this way:

teste.forEach(obj => {
    Object.keys(obj).forEach(key => {
        obj[key].forEach(teste => console.log(teste))
    })
})
  • I understand, but then this structure that I made, you know another way to improve it ? pq the idea is to put two arrays with their respective objects in only one variable, but this way I did this much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.