How to access Object Properties

Hello,
In this exercice:

The solution is:

// 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) {
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
    }
  else if ( prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
    records[id][prop] = [value];
    }
  else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
    }
  else if (value === "") {
    delete records[id][prop];
    }
  
  return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Why instead of : records[id][prop] = value;

records.id.prop = value; Doesn’t works ?

Thanks in advance.

You can’t use dot notation with variables. This has been asked before. Check this out: Record Collection - Dot Notation

I’ve edited your post to add the spoiler tags. If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

I highly recommend against looking at the solutions before you solve the challenge. You can always ask questions here and we’ll help you fix your code.

Hi
Sorry but I don’t see where you are add the tags

Hi Jeremy
ok thank you
Which nationality have you ?

no dot with variables, ok.
In this case why records doesn’t have brackets ?

This syntax is saying that you want to access a property of the records variable. Specifically, you want to access the property with the name stored in the id variable.

above and below the code you posted, you may notice that it is now blurred

Thank you at all. Very clear now!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.