0

I am trying to use map for an array like this one. I cannot predict the name, and there is no common key - just name value pairs, which can be different

{"name":"Text","description":"Text","some other item":"Text"}

I tried various approaches, but most of them require some kind of identifier. It would be great to understand how this can be solved.

2 Answers 2

3

You can use the Object conversion :

Object.values(values).map(e => console.log(e)); //to get a map of the values
Object.keys(values).map(e => console.log(e)); //to get a map of the keys
Object.entries(values).map(e => console.log(e)); //to get a map of [key, value]

You can check theses Object mapping with a live demo here and a documentation here

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

Comments

1

You can use Object.keys() like this:

const obj = {"name":"Text","description":"Text","some other item":"Text"}

Object.keys(obj).forEach(key => {
  console.log(`key: ${key}`)
  console.log(`value: ${obj[key]}`)
})

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.