0

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
2
  • Possible duplicate of Find object by id in an array of JavaScript objects Commented Nov 29, 2018 at 20:04
  • It must be possible since this question is asked and answered about 100 times per day ;) Commented Nov 29, 2018 at 20:05

1 Answer 1

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

thanks Nitish, this works but d isnt initialized, how does it still work. Sorry I am new to this
No problem. This would be the good starting point for you. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.