I have to iterate through json array object.
It has following structure.
var myJSONObject = {
\"abc\": {
\"prod_1\": [
{\"pro
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(", ");
});
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];
}
}
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
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]]);
}
}
}