Skip to content

Instantly share code, notes, and snippets.

@joshuaaguilar20
Created January 29, 2019 22:24
Show Gist options
  • Save joshuaaguilar20/09392dda8cc85b03f87e89d4a798e1cb to your computer and use it in GitHub Desktop.
Save joshuaaguilar20/09392dda8cc85b03f87e89d4a798e1cb to your computer and use it in GitHub Desktop.
Teaching Object CRUD Operations to Students*
// 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"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if(prop!=="tracks"&&value!==""){
var obj = collection[id]
obj[prop] = value
}
else if (prop=="tracks" &&
collection[id].tracks !== undefined && value !==""){
collection[id].tracks.push(value);
}
else if (collection[id].tracks == undefined && prop == "tracks" && value !==""){
var obj = collection[id]
obj[prop] = [];
obj.tracks.push(value);
}else{
var obj = collection[id]
delete obj[prop]
}
return collection;
}
updateRecords(2548, "tracks", "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment