Record Collection - Confused, spoiler also

Record Collection - Confused, spoiler also
0

#1

Tell us what’s happening:
So ultimately I just couldn’t do this. I didn’t even know where to begin. So I googled the answer and now I’m trying to understand it.

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"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
   if(collection[id][prop]) { // here is where my confusion arises
    collection[id][prop].push(value);
   }
   else {
    collection[id][prop]=[value]; // also here
   }
  } else if (value !== "") {
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];
  }

  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

So basically, how does collection[id][prop]; exactly work? is it like accessing with dot notation? Or what? I really don’t understand. Secondly, why does collection[id][prop] = value; without [] one moment, and a few lines above it has []. Any help is appreciated.
Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection


#2

Yes it’s just like dot notation. One thing to remember is you can add new props if they don’t already exist, which is what is happening at the //also here line. What’s confusing here is the [value] part (more on that later).
The reason you use bracket notation is so you can pass the variable values through, otherwise JS will look for a property called id or props, if it’s in [id] it can handle the variable
The first if statement filters to update to the tracks property. All the tracks properties contain an array.

If the property already exists then the value is pushed to the array
if(collection[id][prop]) {
collection[id][prop].push(value); // same as doing collection.id.prop.push(value)
}
if the property doesn’t exist create the property and add the value within a new array. Confusingly bracket notation and arrays use the same bracket type (objects and arrays are related)…
else {
collection[id][prop]=[value]; // same as collection.id.prop = [value]
}


#3

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. Please try not to post full solutions. In the future, if you have specific questions about a specific section of code, then just post that section and ask your question.

Thank you.