Accessing the items inside the nested objects

Tell us what’s happening:
after i have written the code . i f i want to access “album” in id :2468
what should be done :
1----->-- console.log( collection.2468[ “album” ] )

OR

2---->-- console.log(collection [2468] [“album”])

OR

3—>--- console.log(collection.2468.album)

OR
4---->-- for (var element in collection){
console.log(element[2468]["album]
};
please help me in accessing the items inside the nested objects
Your code so far


// Setup
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"
}
};

// Only change code below this line
function updateRecords(id, prop, value) {
if(value === ""){
delete collection[id][prop];
}else if(prop === "tracks"){
collection[id][prop] = collection[id][prop] || [];
collection[id][prop].push(value);
}else {
collection[id][prop] = value;
}

return collection;
}

updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

try them out, what output do you get?

I am not gettin th output
even if i run this commands
2---->-- console.log(collection [2468] [“album”])
// No console output

4---->–for (var element in collection){
console.log(element[2468] [“album”]);
}

//ouput: No console output

5—>-- console.log(collection.2468.album)
//output : SyntaxError: Unexpected token, expected , (48:22)

4—>---for (var element in collection){
console.log(element);
}
// output:

1245
2468
2548
5439

what should be done???

weird, this should have given you "1999"
try removing spaces

this is because you can’t use dot notation with numbers


btw, most of the issues in your code is because you are trying to compare using the assignment operator, so it doesn’t work as you want

1 Like

can u please try on ur side for the same Record Collection code
an post back at any time when its done…

no, I will not solve the challenge for you, if you don’t want to do it you can go to next challenge


most of your issues are because of this, here you are assigning "tracks" to prop not checking if prop is equal to "tracks"

remember = is the assignment operator, == is the comparison operator


try fixing things, and post again your code if you need help again

yeah i did that assigning prop to tracks
after that even i corrected the isssue also…
using === (strict equality operator)
i wiil try again … an post it

i just didnt changed it in the post

hey hi

console.log(collection[“2468”].album)
console.log(collection[“2468”][“album”])
console.log(collection[“5439”][“artist”])
this commands give solution
thanks

got help from scrimba