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

  • Home
  • SEARCH
  • 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 928195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:58:17+00:00 2026-05-15T19:58:17+00:00

I am using JavaScript. I have var myobj = { "one": { "name": "paul",

  • 0

I am using JavaScript.

I have

var myobj = {
    "one": {
        "name": "paul",
        "no"  : "A123"
    },
    "two": {
        "name": "mike",
        "no"  : "B456"
    },
    "three": {
        "name": "andy",
        "no"  : "C789"
    },
};

I have another object newitem that I want to insert

var newIndex = "four";
var newItem = {
    "name": "leo",
    "no"  : "D987"
};

How do I insert newItem into myobj using newIndex as the ‘index’?

By the way, newIndex is not generated by me so I cannot just say myobj.four = newItem; myobj.newIndex = newItem doesn’t work since it uses the word ‘newIndex’ as the index instead of its value.

Any insights?

  • 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-15T19:58:18+00:00Added an answer on May 15, 2026 at 7:58 pm

    Use the bracket notation property accessor:

    myobj[newIndex] = newItem;
    

    It is also useful when you want to define a property whose name is not a valid Identifier, e.g.:

    obj["1foo"] = "bar"; // `obj.1for` will cause a SyntaxError
    obj["a b"] = "bar"; // `obj.a b` will cause a SyntaxError
    

    Note that you can even use an expression with this property accessor, e.g.:

    obj[someFunc()] = "foo";
    

    It will evaluate the expression between the brackets, and it will convert the result to string, because JavaScript doesn’t have true hash tables, property names must be of type String.

    More info:

    • Member Operators
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the javascript module pattern, and have this so far: var APP; if(APP
I have updated my jSon object using the code <script type=text/javascript > jQuery(document).ready(function($){ var
I have a site using javascript popup effect like this: <script type=text/javascript> var GB_ROOT_DIR
I am using JavaScript with jQuery. I have the following var $ticket = $nxt.parent().prev().children('.ticket');
I have a varable var file = "testfile.txt"; I am using the following script
I have var id=ctl03_Tabs1; Using JavaScript, how might I get the last five characters
Say I have the following javasccript var myobj = { name: Julia, birthdate: xxxx,
I have a long text. ex: var text = get snippet text using javascript?;
I'm using JavaScript's .search() function and I have this code: var temp_cc_number = 4111
I have been using JavaScript as a tool for many years now, but only

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.