Issue
I want to print values "Image Management" and "Order Management" from the following JSON. based on Image Management and Order Management I want to list descriptions.
When i tried to print Image Management using foreach it was showing "foreach is not a function()" . error message
I tried the below code.
content.forEach(val => console.log(val));
"content": {
"Image Management": [
{
"code": "image1",
"description": "model image 1"
},
{
"code": "image2",
"description": "model image 2"
}
],
"Order Management": [
{
"code": "order1",
"description": "Tshirt order"
},
{
"code": "order2",
"description": "saree's order"
}
],
}
Solution
You can't iterate an object with forEach
. You can use Object.entries
to convert the object to an array of key value pairs and iterate through that like so:
const content = {
"Image Management": [
{
"code": "image1",
"description": "model image 1"
},
{
"code": "image2",
"description": "model image 2"
}
],
"Order Management": [
{
"code": "order1",
"description": "Tshirt order"
},
{
"code": "order2",
"description": "saree's order"
}
],
}
Object.entries(content).forEach(([key, value]) => console.log(key, value))
Answered By - Haris Bouchlis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.