Record Collection Challenge: Value and [Value];

function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
   if(collection[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;
}

Can you please shed some light on the difference between collection[id][prop] = [value] and collection[id][prop] = value ? The problem has haunted me for few days. Thank you loads!
The code above is from Basic Code Solution in guide section. Have a nice day!

The only difference is when you add the [ and ] around value, you assigning an array of one element (value) instead of just value to collection[id][prop]. The “tracks” property needs to be an array with the first element of value if there is not already a “tracks” property, because if “tracks” already exists, then you know it has to be an array.

Thank you for the clarification! Can I have one more question?

Consider this specific piece of code:

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

In the case the property is not "tracks", is this the part that will update or set the value for that property? Or it does both?

Thank you again for the explanation. Have a nice day!

It does both. You understand it.

1 Like