How do you distinguish properties and values

Tell us what’s happening:
Here is what I don’t get.

if (value === “”) {

delete collection[id][prop]; <---- this says delete property

} else if (prop === “tracks”) {

collection[id][prop] = collection[id][prop] || <----- this says value
= value or value = array

How come in one case ‘collection[id][prop]’ refers to the property (deleting whole property).

and in the other case ‘collection[id][prop]’ refers to the value (value = value or value = .

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"
}
};

// Only change code below this line
function updateRecords(id, prop, value) {

if (value === "") {
delete collection[id][prop];
} else if (prop === "tracks") {
collection[id][prop] = collection[id][prop] || []
collection[id][prop].push(value);}
else collection[id][prop] = value;

return collection;
}

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

Your browser information:

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

Challenge: Record Collection

Link to the challenge:

They refer to the same thing.

delete collection[id][prop];

This says "delete property prop from property id from collection".

collection[id][prop] = collection[id][prop] || [];

This says "update property prop from property id from collection to be an empty array if it is falsey`.

Thank you Jeremy! I’m probably overthinking it, but if I put it into simple algebra terms. The top says delete y

and the bottom says make x = x or x = .

It just confuses me, because they both use the same variable collection[id][prop].

In one case it sees it as deleting the y variable (prop) and in the other case it sees it as adjusting the x variable (value).

Why wouldn’t the top ‘delete collection[id][prop]’ mean delete value? But instead it means delete prop.

JavaScript doesn’t quite give you the language to make the distinction you are trying to make.

Let’s look at a simpler example.

const myObject = {
  prop1 : 'Bob',
  prop2 : 'Smith',
  prop3 : 52,
};

// View myObject
console.log(myObject);

// Update and view myObject
delete myObject["prop1"];
// This removed the property "prop1" from myObject
// Think of this as saying "delete this variable from myObject"
console.log(myObject);


// Update and view myObject
myObject["prop2"] = myObject["prop3"];
// This updated the property "prop2" to have the contents of property "prop3"
// Think of this as saying "update the contents of this variable with the contents of this other variable"
console.log(myObject);

// Update and view myObject
myObject["prop2"] = myObject["prop2"];
// This updated the property "prop2" to have the contents of property "prop2"
// Think of this as saying "update the contents of this variable with the contents of itself"
console.log(myObject);

OHHHHHHH!!! GOT IT! Ok so I can delete everything, or think of it as updating everything. Which means if I update one thing, then the contents will be updated as well.

delete the property and it’s contents or update the property and it’s contents. You basically can just go all the way forward or all the way back.

Thank you Jeremy! Anyway I can show appreciation for your time?

1 Like

I’m happy to have helped : )

Really, I’m just here for those e-high-fives :heart:

Happy coding!