Convert nested JSON object to array JavaScript | Example code

  • by

Use Object values method to convert nested JSON object to array in JavaScript. This method returns an array of a given object’s own enumerable property values.

Object.values(data)

Convert nested JSON object to array JavaScript

Simple HTML example code.

<!DOCTYPE html>
<html>
<body>

  <script>
    var data = {
      code: 42,
      items: {
        item_1: {
          id: 1,
          name: 'foo'
        },
        item_2: {
          id: 2,
          name: 'bar'
        },
        item_3: {
          id: 2,
          name: 'bar'
        }
      }
    };

    const items = Object.values(data.items);
    console.log(items);
  </script>

</body>
</html> 

Output:

Convert nested JSON object to array JavaScript

Do comment if you have any doubts or suggestions on this JS JSON topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.