Home > Back-end >  How to get JSON values in angular
How to get JSON values in angular

Time:07-02

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

CodePudding user response:

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

  • Related