Confused about Record Collection lesson

Hi so why does this code not work

  if (prop !== "tracks" && value !== "") {
    records.id.prop = value;
  } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
    records.id.prop = [value];
  } else if (prop === "tracks" && value !== "") {
    records.id.prop.push(value);
  } else if (value === "") {
    delete records.id.prop;
  }
  return records;
}

the lesson gives the same solution but instead of records.id.prop it uses records[id][prop]

Why can’t you use both bracket and dot notation?
This lesson is taking a lot of energy for me to simply understand.

1 Like

This doesn’t work because records.id.prop is not the same as records[id][prop].

The first is dot notation, looking for a property named id which you are saying is an object with the name prop.

The second is bracket notation, letting you use the variables id and prop as placeholders for the value they contain.

If id contains 3239, then records.id is still looking for a property named id - but records[id] (or records[3239]) is looking fora record with a property name that we can define dynamically, using variable containing a string or number.

2 Likes

thank you for your response
I’m just a little confused, thank you for answering.
What confuses me is that I’ve used dot notation with variables before…

if there is a variable with the name id then shouldn’t you be able to use dot notation? I’ve used it that way before…

var testObj = {
  "hat": "ballcap",
  "shirt": "jersey",
  "shoes": "cleats"
};

var hatValue = testObj.hat;      
var shirtValue = testObj.shirt; 

this works, and you can replace testObj.hat with testObj[hat] so why doesn’t the other one work?

why can’t it resolve the argument with dot notation in the current problem, if this problem I just posted in this post resolves the argument fine?

You’re saying it can’t resolve the argument, but why can’t it resolve the argument? How do these two problems differ?

1 Like

testObj.hat would be the same as `testObj[“hat”]. If i then did this:

// the variable contains the string value
let articleOfClothing = "hat";

// these will all be the same
// door notation...
console.log( testObj.hat );

// bracket notation, with a value
console.log( testObj["hat"] );

// bracket notation, with a variable
console.log( testObj[articleOfClothing] );

In order to use brackets with an object, the value inside the brackets is the string or number that indicates the property name. So in the third one above, i use a variable, but js sees that and retrieves the value stored in that variable ("hat"), and uses it in the brackets.

2 Likes

If you tried testObj[hat] javascript would likely show an error that the variable hat has not been defined.

1 Like

I still don’t really understand.
Is there a part of the lessons that touches on this topic? I can’t really move on until I understand this lesson.

Dot notation only works if you have the exact, literal, character for character name of the property.

You must use bracket notation if the name of your property is stored in a variable.

1 Like

oh ok. So whatever is in dot notation is resolved as the true name of the property, and it doesn’t recognize variables for that reason, but within brackets it can resolve variables.

1 Like

Right. Because the variable is not the true name of the property. The contents of the variable is the true name of the property.

3 Likes

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