Iterate through nested json object array

I have to iterate through json array object.

It has following structure.

var myJSONObject = {
    "abc": {
        "prod_1": [
            {"prod_ver" : "prod 1 ver 1"},
            {"prod_ver" : "prod 1 ver 2"},
        ],
        "prod_2": [
            {"prod_ver" : "prod 2 ver 1"},
            {"prod_ver" : "prod 2 ver 2"},
        ],
        "prod_3": [
            {"prod_ver" : "prod 3 ver 1"},
            {"prod_ver" : "prod 3 ver 2"},
        ]
    }
};

Basically what I am doing is prod_1 is name of a product and the list of versions that prod_1 has are populated inside it.

So now what i want is the name of the product as well as what versions it has.

The problem is there can be many products and many versions under that product. So i need a proper looping structure in javascript that can be generic to handle it.

It would be best if the loop stores product name in one var and it's version in another var as there are some checking i need to on product name.

If the json structure is wrong or a better json structure can be written the please suggest a right/better structure.

Please HELP

Thanks in advance.

Answers:

Answer

Since myJSONObject.abc contains a list of products it would make more sense to define the property abc as an array. Like this:

var myJSONObject = 
{
"abc":
    [
        [
            {"prod_ver" : "prod 1 ver 1"},
            {"prod_ver" : "prod 1 ver 2"},
        ],
        [
            {"prod_ver" : "prod 2 ver 1"},
            {"prod_ver" : "prod 2 ver 2"},
        ],
        [
            {"prod_ver" : "prod 3 ver 1"},
            {"prod_ver" : "prod 3 ver 2"},
        ]
    ]
};

Then you can iterate over the products and their versions using normal loops:

for(var i = 0; i < myJSONObject.abc.length; i++)
{
    var product = myJSONObject.abc[i];
    for(var j = 0; j < product.length; j++)
    {
        var version = product[j];
    }
}

You could take it slightly further and alter your JSON object's structure a bit to make it more easily understandable.

var catalog = 
{
    "products": [
        {
            "name": "prod 1",
            "versions": [
                "ver 1",
                "ver 2"
            ]
        },
        {
            "name": "prod 2",
            "versions": [
                "ver 1",
                "ver 2"
            ]
        }
    ]
};

for(var i = 0; i < catalog.products.length; i++)
{
    var product = catalog.products[i];
    var productName = product.name;
    for(var j = 0; j < product.versions.length; j++)
    {
        var version = product.versions[j];
    }
}
Answer

myJSONObject.abc is an object with keys like prod_1, prod_2, etc. You can loop through the keys of an object using for-in. So:

var productName;
var productVersionArray;

for (productName in myJSONObject.abc) {
    productVersionArray = myJSONObject.abc[productName];
}

Note that the order of the keys is not defined by the specification and will vary from JavaScript engine to JavaScript engine. If you want to do them in a particular order, you have to get an array of them, sort it in the order you want, and then loop through that array. (In an ES5-enabled environment, you can get an array of the keys of an object from Object.keys(yourObject). But you'd need a shim for older browsers.)

Within that loop, you can loop through the array using a standard for loop:

for (versionIndex = 0; versionIndex < productVersionArray.length; ++versionIndex) {
    // Use `productVersionArray[versionIndex].prod_ver` here
}

Here's an example putting it all together:

(function() {

  var myJSONObject = 
    {
    "abc":
        {
            "prod_1": 
            [
                {"prod_ver" : "prod 1 ver 1"},
                {"prod_ver" : "prod 1 ver 2"}
            ],

            "prod_2": 
            [
                {"prod_ver" : "prod 2 ver 1"},
                {"prod_ver" : "prod 2 ver 2"}
            ],
            "prod_3": 
            [
                {"prod_ver" : "prod 3 ver 1"},
                {"prod_ver" : "prod 3 ver 2"}
            ]
        }
    };

  var productName;
  var productVersionArray;
  var versionIndex;

  for (productName in myJSONObject.abc) {
      productVersionArray = myJSONObject.abc[productName];
      display(productName + " has " + productVersionArray.length + " versions listed");
      for (versionIndex = 0; versionIndex < productVersionArray.length; ++versionIndex) {
        display("* " + productVersionArray[versionIndex].prod_ver);
      }
  }

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = String(msg);
    document.body.appendChild(p);
  }

})();

Live Copy | Source

Answer

Updated with ES6

  var { products } =
    {
        "products": [
            {
                "name": "prod 1",
                "versions": [
                    "ver 1",
                    "ver 2"
                ]
            },
            {
                "name": "prod 2",
                "versions": [
                    "ver 1",
                    "ver 2"
                ]
            },
            {
                "name": "prod 3",
                "versions": [
                    "ver 1",
                    "ver 2"
                ]
            }
        ]
    };

    let inventory = products.reduce((accumulator, currentValue) => {
        let { name, versions } = currentValue;
        accumulator[name] = versions
        return accumulator
    }, []);

    Object.entries(inventory).forEach((prod) => {
        let prodName = prod[0];
        let prodVers = prod[1].join(", ");
    });
Answer
function z() {
  for (let key in myJSONObject.abc) {
    let value = myJSONObject.abc[key];
    for (let i = 0; i <= value.length; i++) {
      console.log(value[[i]]);
    }
  }
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.