Record Collection: why use [id]?

I just barely passed all the tests in Record Collection challenge. But i had to peek into other people’s codes to understand how to solve it. I still cannot understand why computer identify [id] as the “5439”, “1245” etc.

In previous challenges we accesed objects with direct names like “ourStorage.cabinet[“top drawer”].folder2”.

Why this sudden change to [id]?
Will this work with other codes the same way?

So far i see the structure like this:

someObject = {
property: value,
property: [value1, value2]
property: value}

So when code gets more complicated are there more properties inside properties?
Or chain will be like [id] - [property] - [value] all the time?

I’m so confused… please help me!

My code


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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.0; rv:52.0) Gecko/20100101 Firefox/52.0.

Review what Randell posted above. To add bit more explanation, suppose if you have an object like this.

const obj = {
  1: "Phillip"
}
console.log(obj["1"])
console.log(obj.1)

The last console log will return an error since you can’t access key of an object using a number. That’s invalid. However, the first console log works just fine. So one reason to use bracket notation is if you have keys of object that are not proper JS variable names.

Therfore you needed to use bracket notations to look at each object inside collection object because their keys were numbers.

Thank you for your soon replies!

Yes, i remember that challenge and looked through it again. But there you needed to add a variable to solve it.

In “Record Collection” i didn’t add any variables, and it still works with [id]

Sorry, still don’t get it…

Function is a piece of code, with given parameters, that is activated when it’s called, right? Does it assign variables to the objects too?

If there will be another function in this challenge but with 2 parameters only (for example “prop” and “value”) what will be then?

The point is - i cannot link [id] to the “5439”, “1245” etc.

You create the function with

function updateRecords(id, prop, value) {...}

And call the function with

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

When the function is called the things written in the parenthesis are passed in the function parameters, as such id is a variable that in this case has value 5439

So, it doesn’t matter how I call the function parameters, because the things written in paranthesis will find it out for me.

I think I’m starting to get it now. Thank you for helping! :grinning:
Will do my best in next challenges.

It doesn’t matter to the code itself, but other humans would greatly appreciate if you used descriptive names

I’m sorry but I still can not understand when id becomes 2548 , a prop becomes “artist”, and a value becomes as “Addicted to Love”.
For example, why if order of parameters changed
function updateRecords(value, id, prop ) {...}
doesn’t work with
updateRecords("ABBA", 5439, "artist" ); ?

it works for that function call

but the tests will use function calls with the arguments in the other order, so if you change the order of the parameters, it will not work

I got it
Many thanks!