1

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"
            }
        ],
}

1 Answer 1

1

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))

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

1 Comment

Thank you @Haris Bouchlis. it is working fine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.