So I understand most of what’s going on with the below solution. Just a quick question, when updating ‘5439’ , obviously the prop ‘artist’ does not exist, so when this piece of code is performed ’ object[id][prop] = value;’. Does it just make the new prop in the class as ‘artist’ does not yet exist.
Just seems weird to me that you don’t need to make that prop first before adding a value to it . Hope this makes some sense.
// Setup
var collection = {
2548: {
albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
albumTitle: '1999',
artist: 'Prince',
tracks: ['1999', 'Little Red Corvette']
},
1245: {
artist: 'Robert Palmer',
tracks: []
},
5439: {
albumTitle: 'ABBA Gold'
}
};
// Only change code below this line
function updateRecords(object, id, prop, value) {
if (prop !== 'tracks' && value !== "") {
object[id][prop] = value;
} else if (prop === "tracks" && object[id].hasOwnProperty("tracks") === false) {
object[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
object[id][prop].push(value);
} else if (value === "") {
delete object[id][prop];
}
return object;
}
updateRecords(collection, 5439, 'artist', 'ABBA');```