I have a nested JSON and I’m getting the "variants" results from API. And I’m trying to get this variants parent names.
Here is the json:
{
"active": true,
"main": [{
"name": "groupOne",
"heights": [{
"name": "sm",
"active": true,
"addSelectedProduct": false,
"colors": [{
"name": "gold",
"triggers": {
"offerIds": ["11111"], "variants": ["11112"]
}
},
{
"name": "blue",
"triggers": {
"offerIds": ["11113"], "variants": ["11114"]
}
},
{
"name": "white",
"triggers": {
"offerIds": ["11115"], "variants": ["11116"]
}
},
{
"name": "black",
"triggers": {
"offerIds": ["11117"], "variants": ["11118"]
}
}
]
},
{
"name": "md",
"active": true,
"addSelectedProduct": false,
"colors": [{
"name": "gold",
"triggers": {
"offerIds": ["11119"], "variants": ["11121"]
}
},
{
"name": "blue",
"triggers": {
"offerIds": ["11122"], "variants": ["11123"]
}
},
{
"name": "white",
"triggers": {
"offerIds": ["11124"], "variants": ["11125"]
}
},
{
"name": "black",
"triggers": {
"offerIds": ["11126"], "variants": ["11127"]
}
}
]
},
{
"name": "lg",
"active": true,
"addSelectedProduct": false,
"colors": [{
"name": "gold",
"triggers": {
"offerIds": ["11128"], "variants": ["11129"]
}
},
{
"name": "blue",
"triggers": {
"offerIds": ["11131"], "variants": ["11132"]
}
},
{
"name": "white",
"triggers": {
"offerIds": ["11133"], "variants": ["11134"]
}
},
{
"name": "black",
"triggers": {
"offerIds": ["11135"], "variants": ["11136"]
}
}
]
}]
},
{
"name": "groupTwo",
"heights": [{
"name": "sm",
"active": true,
"addSelectedProduct": false,
"colors": [{
"name": "gold",
"triggers": {
"offerIds": ["11137"], "variants": ["11138"]
}
},
{
"name": "blue",
"triggers": {
"offerIds": ["11139"], "variants": ["11141"]
}
},
{
"name": "white",
"triggers": {
"offerIds": ["11142"], "variants": ["11143"]
}
},
{
"name": "black",
"triggers": {
"offerIds": ["11144"], "variants": ["11145"]
}
}
]
},
{
"name": "md",
"active": true,
"addSelectedProduct": false,
"colors": [{
"name": "gold",
"triggers": {
"offerIds": ["11146"], "variants": ["11147"]
}
},
{
"name": "blue",
"triggers": {
"offerIds": ["11148"], "variants": ["11149"]
}
},
{
"name": "white",
"triggers": {
"offerIds": ["11151"], "variants": ["11152"]
}
},
{
"name": "black",
"triggers": {
"offerIds": ["11153"], "variants": ["11154"]
}
}
]
},
{
"name": "lg",
"active": true,
"addSelectedProduct": false,
"colors": [{
"name": "gold",
"triggers": {
"offerIds": ["11155"], "variants": ["11156"]
}
},
{
"name": "blue",
"triggers": {
"offerIds": ["11157"], "variants": ["11158"]
}
},
{
"name": "white",
"triggers": {
"offerIds": ["11159"], "variants": ["11159"]
}
},
{
"name": "black",
"triggers": {
"offerIds": ["11161"], "variants": ["11162"]
}
}
]
}]
}]
}
I tried a lot of ways like filter, indexOf but no luck. I can reach the final step with nested foreach functions like that:
var elMain, elHeights, elColor, elOffer;
function findProduct(item){
app.main.forEach(function(el){
el.heights.forEach(function(itemHeights){
itemHeights.colors.forEach(function(itemColor){
if(itemColor.triggers.variants.indexOf(item.toString())){
elOffer = itemColor.triggers.offerIds
elColor = itemColor;
elHeights = itemHeights;
elMain = el;
} else{
}
})
})
})
}
findProduct(11123)
console.log(elMain);
console.log(elHeights);
console.log(elColor);
console.log(elOffer);
But I can’t get the correct result. I need to get selected variant’s parent names directly. Is there a method for that? Any help would be awesome.
2
Answers
You shouldn’t use
forEach
because it doesn’t let you break easily from it when you have found your match. Better use afor (key in obj)
.With
forEach
With
for in
You don’t have to do all that. You can just do
This will exit early if found instead of having to loop through every object every time like the forEach does