I’m trying something like this, but this example does not work.
jsObj = {};
for (var i = 1; i <= 10; i++) {
jsObj{'key' + i} = 'example ' + 1;
}
What can I do to make a dynamic key like this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Square brackets:
In JavaScript, all arrays are objects, but not all objects are arrays. The primary difference (and one that’s pretty hard to mimic with straight JavaScript and plain objects) is that array instances maintain the
lengthproperty so that it reflects one plus the numeric value of the property whose name is numeric and whose value, when converted to a number, is the largest of all such properties. That sounds really weird, but it just means that given an array instance, the properties with names like"0","5","207", and so on, are all treated specially in that their existence determines the value oflength. And, on top of that, the value oflengthcan be set to remove such properties. Setting thelengthof an array to0effectively removes all properties whose names look like whole numbers.OK, so that’s what makes an array special. All of that, however, has nothing at all to do with how the JavaScript
[ ]operator works. That operator is an object property access mechanism which works on any object. It’s important to note in that regard that numeric array property names are not special as far as simple property access goes. They’re just strings that happen to look like numbers, but JavaScript object property names can be any sort of string you like.Thus, the way the
[ ]operator works in aforloop iterating through an array:is really no different from the way
[ ]works when accessing a property whose name is some computed string:The
[ ]operator there is doing precisely the same thing in both instances. The fact that in one case the object involved happens to be an array is unimportant, in other words.When setting property values using
[ ], the story is the same except for the special behavior around maintaining thelengthproperty. If you set a property with a numeric key on an array instance:then (assuming that "200" is the biggest numeric property name) the
lengthproperty will be updated to201as a side-effect of the property assignment. If the same thing is done to a plain object, however:there’s no such side-effect. The property called "200" of both the array and the object will be set to the value
5in otherwise the exact same way.One might think that because that
lengthbehavior is kind-of handy, you might as well make all objects instances of the Array constructor instead of plain objects. There’s nothing directly wrong about that (though it can be confusing, especially for people familiar with some other languages, for some properties to be included in thelengthbut not others). However, if you’re working with JSON serialization (a fairly common thing), understand that array instances are serialized to JSON in a way that only involves the numerically-named properties. Other properties added to the array will never appear in the serialized JSON form. So for example:the value of "objJSON" will be a string containing just
["hello world"]; the "something" property will be lost.ES2015:
If you’re able to use ES6 JavaScript features, you can use Computed Property Names to handle this very easily: