I've below JSON, am facing difficulty to get the key and value dynamically from JSON array:
var productJSON = {
"byProduct": {
"category": {
"Entertainment": {
"TV": 7,
"Speaker": 24,
"DVD Player": 5,
"Home Theater": 4
},
"Home": {
"Sofa Couch": 1,
"TV Furniture": 4
},
"Mobile": {
"iPhone 5s": 1,
"Accessories": 4
}
}
}
}
I'm want key, value from each category and put in two different array, one for product name and another for product count like:
["TV", "Speaker", "DVD Player", "Home Theater","Sofa Couch", "TV Furniture", "iPhone 5s", "Accessories"]
[7, 24, 5, 4,1,4,1,4]
Can someone help how can I get the output like this?
How can I get these value from above JSON array, below is my code but not able to parse all the item from each category:
var ProductJSON = productJSON.byProduct;
var productsName = [], productCount = [];
Object.keys(ProductJSON.category).forEach(function (v) {
var k = Object.keys(ProductJSON.category[v])[0];
productsName.push(k);
productCount.push(ProductJSON.category[v][k]);
});
console.log(productsName);
console.log(productCount);
Thanks for your help in advance.