0

I have this object:

const example = {
  first: [*arrays objects*],
  second: [*arrays objects*],
  third: [*arrays objects*]
}

So, the situation is this: the example has 3 keys. Each key has an array of objects. First i want to loop through the objects keys (first, second, third), then the array of them (with forEach). How can i do that?

1

2 Answers 2

1

To loop an object you need to specify what are you looping, in this case i use the keys

 Object.keys(example).forEach()

Hope this helps :>

const example = {
  first: [{object: '1'},{object: '2'}],
  second: [{object: '3'},{object: '4'}]
}


Object.keys(example).forEach( entrie => {
  console.log(entrie)
  example[entrie].forEach(object => {
    console.log(object)
  })
})

Sign up to request clarification or add additional context in comments.

Comments

1

Object.keys( object ) will give you the keys of an object as an array.

const example = ...
Object.keys( example ).forEach( ( key, index ) => {
    example[key]; // This is your array you wish to loop through.
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.