Objects difference between . and [] Record Collection

Hi evrybody!

First of all sorry for my English.

I’m struggling against a great confusion at a JS challenge and its sintaxis. This one specifically:

I wrote this and it didn’t work (obv).

 function updateRecords(object, id, prop, value) {
  if (prop != "tracks" && value != ""){
       object.id.prop = value;
   } else if (prop == "tracks" && !object.id.hasOwnProperty("tracks")){
     object.id.prop = [value];
   } else if ( prop == "tracks" && value != ""){
     object.id.prop.push(value);
   } else if (value == ""){
     delete object.id.prop;
   }
 
   return object;
 }

After a while i decided to take a look at the hints and I saw that the conditions and if statements were OK and the only difference was that I used dots to access to the objects instead of brackets.

I followed what the previous challenges taught me and I realy didn’t find any difference between trying to access to an object properties using . or

So that’s why I open this topic. I have no problems with the logic of the challenge. My problem is that I cant understand why the function works only with brackets and not with dots as I did in previous challenges.

Thanks you all!

This is the solution:

 function updateRecords(object, id, prop, value) {
   if (prop !== 'tracks' && value !== "") {
     object[id][prop] = value;
   } else if (prop === "tracks" && !object[id].hasOwnProperty("tracks")) {
     object[id][prop] = [value];
   } else if (prop === "tracks" && value !== "") {
     object[id][prop].push(value);
   } else if (value === "") {
     delete object[id][prop];
   }
   return object;
 }

Hi @balbert94!

Welcome to the forum!

I have added the [spoiler] tags to your code for those users who haven’t worked on this challenge yet.

1 Like

the relevant challenge is this one, review it and let us know if you have still doubts

1 Like

omg looks like I’ll have to look for online english courses too hahaha :rofl:

so variables go always inside brackets right? I really didn’t realize that difference.

Thank you very much for answering so quickly.

Should I delete the post? Looks like a pointless question now…

omg looks like I’ll have to look for online english courses too hahaha

Theres soooooo much to keep track of and soooooo many interlocking pieces - don’t be too hard on yourself.

so variables go always inside brackets right?

Yes, if the property name is in stored a variable. The other use case is if the property name is not a valid JS identifier, like starting with a number, has a space in it, etc.

Should I delete the post? Looks like a pointless question now…

No, someone else could benefit.

Also, you wouldn’t be able to delete the post if you wanted to because only moderators can delete posts with replies attached to them.