Created
January 29, 2019 22:24
-
-
Save joshuaaguilar20/09392dda8cc85b03f87e89d4a798e1cb to your computer and use it in GitHub Desktop.
Teaching Object CRUD Operations to Students*
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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