Basic JavaScript: Record Collection. I have a problem

Hello,

I have this code:

// 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 !== "") {

    collection[prop].push(value);
  }
  
  if(prop === "tracks" && collection.hasOwnProperty("tracks") === false) {

    collection.tracks = [];
    collection.tracks.push(value);
  }

   if(prop === "tracks" && value !== "") {
     collection.tracks.push(value);
   }

   if(value === ""){
     delete collection[prop];
   }

  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

I am getting error: Cannot read property ‘push’ of undefined

Any ideas?

Here on line 35-

image

image

You’re trying to access the property “artist” of the object collection. That property doesn’t exist. You need to first make use of the id argument you’re passing in to the updateRecords function.

Thanks. How do I make use of the id argument?

You can chain together property accessors to go through the different levels in nested objects. In this case-

collection[id][prop]

will get you the array you are looking to push to.

Thanks.

OK, so now I have this code:

// 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 !== "") {

    collection[id][prop].push(value);
  }
  
  if(prop === "tracks" && collection.hasOwnProperty("tracks") === false) {

    collection.id.tracks = [];
    collection.id.tracks.push(value);
  }

   if(prop === "tracks" && value !== "") {
     collection.id.tracks.push(value);
   }

   if(value === ""){
     delete collection[id][prop];
   }

  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

but I still get error: Cannot read property ‘push’ of undefined

Ah I see.

push() is used to add a value to an existing array. That’s not what you’re trying to do in this instance.

With updateRecords(5439, "artist", "ABBA"), you’re trying to add a new key value pair to the object “5439”.

Instead of using push(), you need to use an assignment operator =.

Thanks, I see.

Here is my code now:

// 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 !== "") {

    collection[id][prop] = value;
  }
  
  if(prop === "tracks" && collection.hasOwnProperty("tracks") === false) {

    collection.id.tracks = [];
    collection.id.tracks.push(value);
  }

   if(prop === "tracks" && value !== "") {
     collection.id.tracks.push(value);
   }

   if(value === ""){
     delete collection[id][prop];
   }

  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

I get error: Cannot set property ‘tracks’ of undefined

Thanks.

Here is my code now:

// 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 !== "") {

    collection[id][prop] = value;
  }
  
  else if(prop === "tracks" && collection.hasOwnProperty("tracks") === false) {

    collection[id].tracks = [];
    collection.id.tracks.push(value);
  }

   else if(prop === "tracks" && value !== "") {
     collection[id].tracks.push(value);
   }

   else if(value === ""){
     delete collection[id][prop];
   }

  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

I get error: Cannot read property ‘tracks’ of undefined

Thanks.

Here is my code now:

// 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 !== "") {

    collection[id][prop] = value;
  }
  
  else if(prop === "tracks" && collection.hasOwnProperty("tracks") === false) {

    collection[id].tracks = [];
    collection[id].tracks.push(value);
  }

   else if(prop === "tracks" && value !== "") {
     collection[id].tracks.push(value);
   }

   else if(value === ""){
     delete collection[id][prop];
   }

  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

I get errors:

After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.

After updateRecords(2548, “tracks”, “”), tracks should not be set

this will always be false

Thanks. Why? And, how do I fix that?

heck carefully what you have written. Does collection have a tracks property?

It has a tracks property. Only the last object lacks a tracks property.

are you really sure that collection has a tracks property?

so, if you do collection.tracks you should get something.

I don’t get it, can you please explain? Tracks is in collection[id].tracks.

I turned this:

into this: if( collection[id].hasOwnProperty(“tracks”) === false

and now it works.

1 Like