Basic JavaScript - Record Collection Clarification Needed

I just have a question regarding how I did things.

function updateRecords(records, id, prop, value) {
if (value == “” ){
delete records[id][prop];
}
return records;
}

This code worked for me but previously I had tried:

delete records.id.prop;

Could someone clarify to me why the second one(above) did not work?

many thanks!

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

Dot notation cannot work with a variable holding the name of the property. If you are using a variable to say what property you are accessing, bracket notation is mandatory.

This is a useful explainer I think:

When you say “variable holding the name of a property” you mean in this case either ID or PROP?
And sorry but just to clarify, why is this example ok then?

const ourStorage = {
  "desk": {
    "drawer": "stapler"
  },
  "cabinet": {
    "top drawer": { 
      "folder1": "a file",
      "folder2": "secrets"
    },
    "bottom drawer": "soda"
  }
};

ourStorage.cabinet["top drawer"].folder2;
ourStorage.desk.drawer;

See at the end the tutorial was accessing ourStorage.desk.drawer; which would come out to be “stapler”, why is this considered different ?

This helped alot thank you. So I can only recall keys assigned through variables via bracket recall.

So am I correct in saying when :
function updateRecords(records, id, prop, value)
occured it created the variables id, prop, value and because this was created then the only way to recall was via bracket notation?

Yes, if you are using function parameters (or any other variables) rather than explicit object properties, then you must use bracket notation.

Thanks, and just one last question , is this an easy mistake to make for a beginner or was this distinction meant to be easy enough for me to spot the first time by myself? I felt stupid I couldn’t figure it out at first.

No, there’s so much stuff to learn that we can’t possibly be expected to understand or remember everything. I think the dot/bracket distinction is explained somewhere in the objects part of the Javascript curriculum, but I can’t remember where to be honest.

You should always feel completely free to ask questions about anything which you don’t understand - that’s what this community is here for!

2 Likes

Both id and prop are variables. That means they can be used as properties in dot notation

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.