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”?
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:
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"] ,
};