0

I have an Object:

Object{
         "field1":"fill this field!",
         "field1":"fill this field!",
         "field1":"fill this field!"
}

I need to access the key and value of this Object. How can I transform this Object in Array to use forEach.

2 Answers 2

4

No need to "transform" it, you can loop on the keys directly.

Object.keys(yourObject).forEach( key => {
     console.log(yourObject[key]); //value    
     console.log(key); //key
});
Sign up to request clarification or add additional context in comments.

2 Comments

Doing this I get the value ok? And how can I get the key?
You have the key in the loop.
0

You can list the object's keys and place the values in a new array.

  const _versions = [];
  Object.keys(versions).forEach(v => _versions.push(versions[v]));
  this.versions = _versions;

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.