Really need help with record collection problem

Hey guys I’m struggling a bit with this problem. In the code solution for this particular problem, where is he creating the array, when the prop is ‘tracks’ but the object that is being updated doesn’t have a ‘tracks’ array property? And what condition checks to see if the object doesn’t have a ‘track’ prop? Heres his solution:

function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
   if(collection<a href='https://forum.freecodecamp.com/images/emoji/twitter/rocket.png?v=3 ":rocket:"' target='_blank' rel='nofollow'>id][prop]) {
    collection[id][prop].push(value);
   }
   else {
    collection[id][prop]=[value];
   }
  } else if (value !== "") {
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];
  }

  return collection;
}

Also here’s my solution, I am failing one test— After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.—

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

  return collection;

I don’t know what I’m doing. I tried what you said but my code still doesn’t work.

function hasTrackProp(id) {
  if(collection[id]['track'] == undefined) {
    return true;
  }
}

function updateRecords(id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    collection[id][prop] = value;
  }
  else if (prop === 'tracks' && value !== '') {
   if(hasTrackProp(id)) {
     var newArray = [0];
     var updatedArray = newArray.push(value);
     collection[id][prop].push(updatedArray);
   }
   collection[id][prop].push(value);
  }
  else if (value !== '') {
    collection[id][prop] = [value];
  }
  else delete collection[id][prop];

  return collection;
}

Thank you so much I finally solved it, I’m gonna pick this one apart till I understand it completely. Starting with:

collection[id][prop] = value; VS collection[id][prop] = [value]; ----what’s the difference?

also is collection[id][prop] the same as collection[id].[prop] or collection.id.prop?

I guess I can tweak the code and see, but thanks again, you helped a lot!

oh ok, heres my solution that worked

function updateRecords(id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    collection[id][prop] = value;
  }
  else if (prop === 'tracks' && value !== '') {
   if(!collection[id].hasOwnProperty('tracks')) {
     var newArray = [];
     newArray.push(value);
     collection[id][prop] = newArray;
   }
  else collection[id][prop].push(value);
  }
  else if (value !== '') {
    collection[id][prop] = [value];
  }
  else delete collection[id][prop];

  
  return collection;
}

Though i noticed when I take out that last if else statement, making the code:

function updateRecords(id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    collection[id][prop] = value;
  }
  else if (prop === 'tracks' && value !== '') {
   if(!collection[id].hasOwnProperty('tracks')) {
     var newArray = [];
     newArray.push(value);
     collection[id][prop] = newArray;
   }
  else collection[id][prop].push(value);
  }
  else delete collection[id][prop];

  
  return collection;
}

It still works, why is this piece of code unnecessary. I only have it in there because it was in the solution that they gave me.