Record Collection Basic Javascript

Tell us what’s happening:
Hello,

I need a little help with this challenge, I don’t quite get the grasp of it and I don’t want to watch the hint.
Can’t quite understand this part and how to code it: “If prop is "tracks" but the album doesn’t have a "tracks" property, create an empty array before adding the new value to the album’s corresponding property.”
Any help will be appreciated. Thanks

Your code so far


// 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.tracks = [];
  }else if(prop === "tracks" && value !== ""){
    collection[id][prop].push(value);
  }else if(value === ""){
    delete collection[id][prop];
  }
  
  return collection;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection/

So if the prop being passed in is "tracks", but the object pertaining to the id being passed in doesn’t have a tracks property on it. Then first set the tracks property to an empty array. Then add the value to this empty array.

For example, in the collection object, look at id "5439". There isn’t a “tracks” property so you’d have to first set the tracks property on it to an empty array and then add the value to that array.

I’ve update the function but now i get 'cannot read the property ‘push’ of undefined.

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

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

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

It’s telling you that there is no tracks array for the id you specified so there is nothing to ‘push’ to. (the push method only works on arrays). If there is no array already present, you need to create an empty array before you can push.

here’s a hint:

const a={} //create an object called a
a.b=[] //a.b now has an empty array

Right, that’s because that prop does not exist. So you have to create a blank array for it first, and then push the value into it.

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

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

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

I understood the part with the empty array and i created one, hope i did good. The update code returns this:
’ After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.

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