Tell us what’s happening:
Describe your issue in detail here.
**Your code so far**
// Setup
var recordCollection = {
2548: {
albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
albumTitle: '1999',
artist: 'Prince',
tracks: ['1999', 'Little Red Corvette']
},
1245: {
artist: 'Robert Palmer',
tracks: []
},
5439: {
albumTitle: 'ABBA Gold'
}
};
// Only change code below this line
function updateRecords(records, id, prop, value) {
console.log(records[id])
return records;
}
updateRecords(recordCollection, 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/91.0.4472.124 Safari/537.36
Challenge: Record Collection
Link to the challenge:
Hey,
so I was trying to console.log(records.id) and it shows undefined. Whereas when I tried the earlier exercises on nested objects I used the same notation and had no problem.
But surprisingly enough, console.log(records[id]) works.
Could someone point me towards what I am missing here?
Thanks in advance.
Hey There @bhishmadas9
If you try to use dot notation with the variable id, the browser will search for a property in the object called "id"
In bracket notation, you must use “” when mentioning a object property name.
Bracket notation also allows you to use variable like id in this case
To use a variable to access an object property, you must not use “” object[id] //This searches for the value of variable id
object.id // This searches for the property "id" in the object
hey,
Thanks for the quick reply. Really appreciate it.
But could you please tell me why console.log(records.id) doesn’t work?
Sorry, I am a bit confused here. Would be grateful if someone clarified.
Thanks.
… it will look if the records object has a property that is literally called id. Like this:
const records = {
id: 1234
}
But the id in this challenge is a variable. It can have many different values. So you can’t access the property with dot notation, you need bracket notation:
Yes exactly. You use dot notation when you know the exact literal name of the property you’re trying to access (you can also use bracket notation with quotes, see second example below, and you must use it when the property begins with a number or contains white space).
If you have the property name stored in variable, you have to use bracket notation without quotes.