I have a snippet of code that updates records in a json object representing a musical album collection. The function written here updates the records given an album ID, a property ("tracks"), and a value ("albumName")
The error I get below is:
"message": "Uncaught TypeError: collection[id][prop].push is not a function",
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];
return collection;
}
if(prop === "tracks"){
if(!collection[id].hasOwnProperty('tracks')){
collection[id].tracks="";
}
if(value){
collection[id][prop].push(value);
}
} else {
collection[id][prop] = value;
}
return collection;
}
updateRecords(5439, "tracks", "Take a Chance on Me");
Some other test case scenarios

collection[id][prop]should be an array and I tested it in console seperately, not sure where the problem is