Created
September 4, 2016 20:22
-
-
Save jgresalfi/1588ece26618998d77b9a8eb990c7132 to your computer and use it in GitHub Desktop.
Modifying objects/checking properties - record collection example
This file contains 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
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" | |
} | |
}; | |
function updateRecords(id, prop, value) { | |
var isTracks = collection[id].hasOwnProperty("tracks"), | |
newArray = []; | |
if (prop === "album" || prop === "artist") { | |
collection[id][prop] = value; | |
} else if (prop === "tracks" && isTracks) { | |
collection[id][prop].push(value); | |
} else if (isTracks !== false) { | |
collection[id][prop] = newArray; | |
collection[id][prop].push(value); | |
} | |
return collection; | |
} | |
updateRecords(5439, "tracks", "ABBA"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment