Is it possible to loop through an array of objects containing arrays to find a particular array. So in other words, I already have a variable a which has the value "60749428", so I am trying to say find a which is "60749428" in the "available" array.This is the code sample below:
"values": {
"available": [
{
"60750276": [
{
"count": 11,
"name": "16",
"percentage": 84.6153846153846,
"value": "16"
},
{
"count": 11,
"name": "16.0.1",
"percentage": 84.6153846153846,
"value": "16.0.1"
},
{
"count": 12,
"name": "16.2",
"percentage": 92.3076923076923,
"value": "16.2"
},
{
"count": 7,
"name": "16.2.4",
"percentage": 53.8461538461538,
"value": "16.2.4"
}
]
},
{
"69127027": [
{
"count": 8,
"name": "65",
"percentage": null,
"value": "65"
},
{
"count": 4,
"name": "69",
"percentage": null,
"value": "69"
}
]
},
{
"60749428": [
{
"count": 8,
"name": "How To",
"percentage": 61.5384615384615,
"value": "How To"
},
{
"count": 4,
"name": "Training",
"percentage": 30.7692307692308,
"value": "Training"
}
]
}
]
}
asked Nov 29, 2018 at 19:54
-
Possible duplicate of Find object by id in an array of JavaScript objectszfrisch– zfrisch2018年11月29日 20:04:30 +00:00Commented Nov 29, 2018 at 20:04
-
It must be possible since this question is asked and answered about 100 times per day ;)zfrisch– zfrisch2018年11月29日 20:05:07 +00:00Commented Nov 29, 2018 at 20:05
1 Answer 1
Looking at the code sample, it looks like you can use Array.find to achieve this
let values = { "available": [ { "60750276": [ { "count": 11, "name": "16", "percentage": 84.6153846153846, "value": "16" }, { "count": 11, "name": "16.0.1", "percentage": 84.6153846153846, "value": "16.0.1" }, { "count": 12, "name": "16.2", "percentage": 92.3076923076923, "value": "16.2" }, { "count": 7, "name": "16.2.4", "percentage": 53.8461538461538, "value": "16.2.4" } ] }, { "69127027": [ { "count": 8, "name": "65", "percentage": null, "value": "65" }, { "count": 4, "name": "69", "percentage": null, "value": "69" } ] }, { "60749428": [ { "count": 8, "name": "How To", "percentage": 61.5384615384615, "value": "How To" }, { "count": 4, "name": "Training", "percentage": 30.7692307692308, "value": "Training" } ] } ] }
let res = values.available.find(d => d[60749428])
console.log(res)
answered Nov 29, 2018 at 19:58
2 Comments
teestunna
thanks Nitish, this works but d isnt initialized, how does it still work. Sorry I am new to this
Nitish Narang
No problem. This would be the good starting point for you. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
lang-js