SHARE
TWEET

Untitled

a guest Nov 19th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var collection = {
  2.    2548: {
  3.      album: "Slippery When Wet",
  4.      artist: "Bon Jovi",
  5.      tracks: [
  6.        "Let It Rock",
  7.        "You Give Love a Bad Name"
  8.      ]
  9.    },
  10.    2468: {
  11.      album: "1999",
  12.      artist: "Prince",
  13.      tracks: [
  14.        "1999",
  15.        "Little Red Corvette"
  16.      ]
  17.    },
  18.    1245: {
  19.      artist: "Robert Palmer",
  20.      tracks: [ ]
  21.    },
  22.    5439: {
  23.      album: "ABBA Gold"
  24.    }
  25. };
  26.  
  27. function updateRecords(id, prop, value) {
  28.    
  29.    var check = collection[id].hasOwnProperty("tracks");
  30.  
  31.    switch (prop) {
  32.       case "album":
  33.          collection[id][prop] = value;
  34.          break;
  35.       case "artist":
  36.          if (value == "") {
  37.             delete collection[id][prop];
  38.          } else {
  39.             collection[id][prop] = value;
  40.          }
  41.          break;
  42.       case "tracks":
  43.          if (value == "") {
  44.             delete collection[id][prop];
  45.          } else if (check == true) {
  46.             collection[id][prop].push(value);
  47.          } else {
  48.             collection[id][prop] = [value];        
  49.          }
  50.          break;
  51.    }
  52.  
  53.    console.log(collection);
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top