Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 682447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:35:48+00:00 2026-05-14T01:35:48+00:00

I’m trying something like this, but this example does not work. jsObj = {};

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:35:49+00:00Added an answer on May 14, 2026 at 1:35 am

    Square brackets:

    jsObj['key' + i] = 'example' + 1;
    

    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 length property 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 of length. And, on top of that, the value of length can be set to remove such properties. Setting the length of an array to 0 effectively 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 a for loop iterating through an array:

    for (var i = 0; i < myArray.length; ++i) {
      var value = myArray[i]; // property access
      // ...
    }
    

    is really no different from the way [ ] works when accessing a property whose name is some computed string:

    var value = jsObj["key" + i];
    

    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 the length property. If you set a property with a numeric key on an array instance:

    myArray[200] = 5;
    

    then (assuming that "200" is the biggest numeric property name) the length property will be updated to 201 as a side-effect of the property assignment. If the same thing is done to a plain object, however:

    myObj[200] = 5;
    

    there’s no such side-effect. The property called "200" of both the array and the object will be set to the value 5 in otherwise the exact same way.

    One might think that because that length behavior 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 the length but 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:

    var obj = [];
    obj[0] = "hello world";
    obj["something"] = 5000;
    
    var objJSON = JSON.stringify(obj);
    

    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:

    var key = 'DYNAMIC_KEY',
        obj = {
            [key]: 'ES6!'
        };
    
    console.log(obj);
    // > { 'DYNAMIC_KEY': 'ES6!' }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you could do something like this: ViewData["accountlist"] = new SelectList((from… May 14, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer Why don't you create a CLR SP using C# and… May 14, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer I found by myself the answer to my question: attached… May 14, 2026 at 6:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.