Skip to content

Instantly share code, notes, and snippets.

@f0xtek
Created February 10, 2019 22:27
Show Gist options
  • Save f0xtek/688df3781d931668c8145d2dcaff6549 to your computer and use it in GitHub Desktop.
Save f0xtek/688df3781d931668c8145d2dcaff6549 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zuxoteq
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
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"
}
};
var collectionCopy = JSON.parse(JSON.stringify(collection));
function updateRecords(id, prop, value) {
if (prop !== "tracks" && value !== "") {
collection[id][prop] = value;
} else if (prop === "tracks" && collection.hasOwnProperty("tracks") === false) {
collection[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
collection[id][prop].push(value);
}
if (value === "") {
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
console.log(updateRecords(2468, "tracks", "Free"));
</script>
<script id="jsbin-source-javascript" type="text/javascript">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"
}
};
var collectionCopy = JSON.parse(JSON.stringify(collection));
function updateRecords(id, prop, value) {
if (prop !== "tracks" && value !== "") {
collection[id][prop] = value;
} else if (prop === "tracks" && collection.hasOwnProperty("tracks") === false) {
collection[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
collection[id][prop].push(value);
}
if (value === "") {
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
console.log(updateRecords(2468, "tracks", "Free"));
</script></body>
</html>
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"
}
};
var collectionCopy = JSON.parse(JSON.stringify(collection));
function updateRecords(id, prop, value) {
if (prop !== "tracks" && value !== "") {
collection[id][prop] = value;
} else if (prop === "tracks" && collection.hasOwnProperty("tracks") === false) {
collection[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
collection[id][prop].push(value);
}
if (value === "") {
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
console.log(updateRecords(2468, "tracks", "Free"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment