0

I'm using Node JS packages and below is the help I want

I have the following JSON data.

[{
"name":"IronMan",
"description":"Genius",
"head_id":"34",
"domain_name":"ironman.com"
}]

I want to access the Value of the keys inside a loop. I don't know how to loop through the above JSON data.

1
  • Using Node? or simply JS? Commented Apr 27, 2020 at 8:14

2 Answers 2

2
const parsedData= JSON.parse(data) // This will parse your stringifyed Array of objects

parsedData.forEach(entry=> {
// looping through each object in the array and accessing its keys using Object.values()
  Object.values(entry).forEach(value=> {
     console.log(value) // here you will get all the values of all the objects in an d 
  })
})


// You can also use 
Object.keys(entry).forEach(key => {
// key = 'name'
    console.log(entry[key]))
}
// You can also use 
Object.entries(entry).forEach(keyValuePair =>{
 // keyValuePair = ['name', 'IronMan']
  console.log('key', keyValuePair[0])
  console.log('value', keyValuePair[1])
})
Sign up to request clarification or add additional context in comments.

Comments

1

Learn about the for...in loop then.

let stuff=[{
"name":"IronMan",
"description":"Genius",
"head_id":"34",
"domain_name":"ironman.com"
}];

for(let item of stuff) // this is not the one, just example data is an array
  for(let key in item) // this is the one
    console.log(key,item[key]);

1 Comment

Thank you. Even this helped and produced the same output. :) :D

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.