I'm stuck in recod collection tutorial

How to iterate through collection nested objects?

basic-javascript/record-collection

what’s your code? what have you tried so far?

(also, do you really need to iterate through the objects? you have the id parameter - what’s its use in the object?)

anyway, to iterate through an object you can use a for...in or for...of loop, depending on what you need (check them in the documentation)

it is json object :

var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};

does the code below is enough to get a specific object?

if(collection.id==id){
   ///
 }

no, because collection.id is undefined as collection doesn’t have an id property

var collection = {
   "2548": ...;
   "2468": ...;
   ...
}

this is the structure of collection
you don’t need an if statement- how can you use id? what is it rapresenting?

1 Like
`collection[id]`   will get a specific object within the collection.

so you already have the object on which you need to operate using prop and value, you don’t need to loop over all the inner-objectsm right?

1 Like