0

Nothing I try works, and non of the millions of questions related to this topic actually asks this very same question.

let userList = {
    user1: "tom"
}

for(let i = 0; i < 5; i++) { 
  userList[[user[i]]] = i
}

Why is this not working?

It keeps giving me the user is not defined error. Why? Why is it not adding the new property? It works if I do it outside the loop, but not inside. Why?

How can I dynamically add new properties? I wanna add user1/2/3/4/5/ etc.

happy_story
  • 1
  • 1
  • 7
  • 17
  • Can you provide an example of what you are expecting the code to do / what you are trying to achieve? – Alvie Mahmud Jun 29 '21 at 12:29
  • 1
    `userList['user' + i] = i` – Ivar Jun 29 '21 at 12:30
  • @Ivar Can I ask you two more questions, Ivar? One, why does saying `userList.user1 = "some"` outside the loop works, in the sense that, it creates the new property, but when you do it inside the loop, `userList.user[i] = 'x'` it doesn't? It gives you again `user undefined`? And the second question is, why is there so much complete rubbish and contradictory information everywhere? Your answer is not found ANYWHERE, literally, yet some muppet again closed my question and re-directed me to a question I already looked at, and gives wrong answer? – happy_story Jun 29 '21 at 13:09
  • 1. Because `user[i]` looks in the `user` property of `userList` for a key/index `i`. `user[i]` doesn't concatenate `user` and `i` to make a new `user1` key. If you want to do so, you need to create a string that contains the full key name/identifier, and then use the bracket notation property accessor to access the property based on that string. In your code, `user` is treated as separate entity, which doesn't exist. – Ivar Jun 29 '21 at 13:21
  • 2. I can't really tell you why there is so much rubbish online. I do know that Stack Overflow has a [duplicate system](https://stackoverflow.com/help/duplicates) that _does_ aim to reduce this noise by bundling all answers to the same-ish questions in one place, where the best one rises to the top. You can partly blame me for the chosen duplicate, as I contributed to it, since I believe that by understanding that question well enough, it should be possible to figure out how it works. – Ivar Jun 29 '21 at 13:22
  • As a matter of fact, I believe the [third answer](https://stackoverflow.com/a/24514304) in the duplicate, suits your needs pretty well. – Ivar Jun 29 '21 at 13:25
  • Small correction to my first clarifying comment: `userList[[user[i]]]` doesn't look for a `user` in `userList`. It actually first tries to resolve `user[i]` (which will already fail as `user` doesn't exist), then use its result to wrap it in an array (the middle `[` and `]` and finally use that resulting array as the key for `userList`. – Ivar Jun 29 '21 at 13:29
  • So, first of all, what's the difference between `key` and `property`? I've always referred to the property name of an object, in my case `user` as `property`. Is it called `key`? Also, `userList[[user[i]]]` this I read in one of my many questions I read through in search for an answer to my question. They said that, this is how you add new properties to an object, but apparently they were wrong. So, what does it mean to try to `resolve` the `user[i]`? Resolve what? You mean access the property (or key)? Not that I think about it, how is this even supposed to work, since `[]` this is ... – happy_story Jun 29 '21 at 13:48
  • ..is property notation for accessing the values of properties, correct? Could you add a string inside? I guess I don't understand the bracket notation as good as I thought I did. – happy_story Jun 29 '21 at 13:50
  • An object is a collection of key/value pairs. The combination of a key/value pair is called a property. So you have an object `{name: 'Ivar', site: 'SO'}`, then `name` is a key, `'Ivar'` is a value, and `name: 'Ivar'` as a whole is a property of the object. And with "resolve" I mean "execute". To show you the order in which the expressions are evaluated. In `userList[[user[i]]]`, first the value of `i` is is evaluated. The result is passed to `user[...]`, whose result is then passed to `[...]`, whose result is finally passed `userList[...]`. – Ivar Jun 29 '21 at 13:58
  • okay, so.. remind me again, If I try to just access the properties, or the value of each `key`, why is saying `user[i]` not working, despite having a `key` called `user1`? I just tried doing this: `console.log(userList.user[i])`, and it didn't work. What does the bracket notation do here? – happy_story Jun 29 '21 at 14:07
  • The bracket notation is a property accessor. It tries to access the property with key `i` of the `user` property. It is the same as the "dot notation", but it also works for dynamic values and invalid identifiers (such as numbers). If `user1` were `userOne`, then `userList.user['One']` would be the same as `userList.user.One`, not `userList.userOne`. If `userList = {user: {'1': 'user 1'}}`, then it will work fine. But `userList` doesn't have a `user` property, so `userList.user` returns `undefined`. – Ivar Jun 29 '21 at 14:17
  • Okay, so I think I understand, but it's the terminology that it confusing me still a bit. When you say you are `accessing a property` what do you mean by that? If `user1 = 'some` as a whole is called a `property`, how exactly can you access it? By saying `userList.user1` what am I doing here? Am I accessing the `key` called `user1` or am I accessing the entire property of `user1 = 'some'`? Do you access the a property by targeting its `key`? Does every time you access a property you only get the `value` of the property? Sorry if these questions sound too beginner-ish. – happy_story Jun 29 '21 at 14:48
  • "_By saying `userList.user1` what am I doing here?_". Exactly. That is called a [property accessor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors). "_Do you access the a property by targeting its key?_" Yes. "_Does every time you access a property you only get the value of the property?_" Correct. You access the property by key, which returns its value. "_Am I accessing the `key` called `user1` or am I accessing the entire property of `user1 = 'some'`?_". The "key" is merely an identifier that you use access the right property. – Ivar Jun 29 '21 at 14:59
  • 1
    Okay, thanks a lot. I think I got it for now :) Really appreciate your help. – happy_story Jun 29 '21 at 15:14

0 Answers0