Simple, beginner question about Objects in "JavaScript Basics" Course

I am curious about a detail in this code :

function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
  } else if (prop === "tracks" && value !== "" && records[id].hasOwnProperty("tracks") === false) {
    records[id][prop] = [value];  // <--------Here is the detail.
  } else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
  } else if (value === "") {
    delete records[id][prop];
  }
  return records;
}

Why do I have to mention the [prop] to assign a new property and add a value to it , when the property is not existent in the object.

Thank you , for your time ! :smiley:

1 Like

I’m not quite sure I understand. How would you add a property to an object if you didn’t specify what the property was called?

eg if I wanted to add a property called “tracks”, how would I be able to do that if I didn’t specify that I wanted to add a property called “tracks”?

What I see - with the line ( records[id][prop]…) - is that im trying to access a property which in this case is not there (prop=“tracks”) .

So what you are saying is that if there is no property like that , it is also an assingment tool ?

I’m still not quite sure where the misunderstanding is here, can you show the exact bit that’s confusing you? It’s exactly the same as variables, ie

Assign “value” to whatever prop is:

obj[prop] = "value";

Access whatever the value of whatever name prop is:

obj[prop]

If there is a object like this one :

const  someObject= { someProp: { prop1 , prop2}}

To access properties of the object : someObject[someProp][prop1];

The thing that confuses me , if I try to assign a “prop3” . Shouldn’t I be assigning it like this:

someObject[someProp]=[prop3];   /* to access the branch (someProp) where the prop3 should be */

Is it always this hard to explain your logic of the code ? (Bad logic :sweat_smile:)

If you have an object like in your example:

const someObject = { 
  someProp: { 
    prop1: "some value for prop1" , 
    prop2: "some value for prop2",
  }
}

So your variable someProp is the key “someProp” and your variable prop1 is the key “prop1”, then someObject[someProp][prop1] refers to the value “some value for prop1”.

But assigning, again, it’s just assigning values, it doesn’t work any differently with objects than it does with variables.

So this:

someObject[someProp]=[prop3]

prop3 has to be some variable you have, so say it’s “some value for prop3”, you’re assigning an array with that in to someObject.someProp: substituting the actual example values in instead of variables:

someObject.someProp = ["some value for prop3"]

You’re just saying "replace whatever the value is for someObject.someProp with that array, it mutates your example object from this:

const someObject = { 
  someProp: { 
    prop1: "some value for prop1" , 
    prop2: "some value for prop2",
  }
}

To this:

const someObject =  { 
  someProp: ["some value for prop3"] ,
};
1 Like

@DanCouper thank you very ,very much for your contribution and time!

Is it okay for me to post here, beginner questions like these ?

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