Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get JSON values in angular

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading