Issue
I have this array and I want to console log item
value. How can I do that? I got this array from MongoDB. I would appreciate any help.
{
"_id" : "61462a7bf3c0be993bcfdc3e",
"item": "journal",
"qty": 25,
"size": {
"h": 14,
"w": 21,
"uom": "cm"
},
"status": "A"
}
Edit:
I looked in MongoDB documentation and found the solution. Thanks to everyone who answered.
Solution
I think you can simply access the key from the object like this:
const obj = {
"_id" : "61462a7bf3c0be993bcfdc3e",
"item": "journal",
"qty": 25,
"size": {
"h": 14,
"w": 21,
"uom": "cm"
},
"status": "A"
}
console.log(obj.item);
Another way could be to find out the keys and iterate over them if keys are dynamic in nature:
const obj = {
"_id" : "61462a7bf3c0be993bcfdc3e",
"item": "journal",
"qty": 25,
"size": {
"h": 14,
"w": 21,
"uom": "cm"
},
"status": "A"
}
const keys = Object.keys(obj);
keys.forEach(key => console.log(`Key is ${key} and the value is ${JSON.stringify(obj[key])}`))
Answered By – Apoorva Chikara
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0