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 8344431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:22:13+00:00 2026-06-09T06:22:13+00:00

I was curious about adding elements in to an associative array using javascript, var

  • 0

I was curious about adding elements in to an associative array using javascript,

var text=[{"key":"1", "value":"no"},{"key":"2", "value":"yes"} ];

what if I want to add an element in to above array… say to second location,

text[1].key="4";
text[1].value="test";

I ve tried it this way,

test.splice(parseInt(1), 0 );
test[1].type="4";
test[1].value="test";
  • 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-06-09T06:22:16+00:00Added an answer on June 9, 2026 at 6:22 am

    Here’s some example code (with which you can play around here):

    function toString(array) {
        var output = '';
        for(i = 0; i < array.length; i++) {
            if(output != '')
                output += ' - ';
            output += array[i].key;
        }
        return output;
    }
    
    var array = [{"key":"1", "value":"no"},{"key":"2", "value":"yes"} ];
    
    test = {"key":"4", "value":"test"};
    
    alert(toString(array));
    array.splice(1, 0, test);
    alert(toString(array));
    

    The array.splice() method takes the following parameters (taken from here):

    array.splice(index,howmany,item1,.....,itemX)
    
    • index: An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array.
    • howmany: The number of items to be removed. If set to 0, no items will be removed.
    • itemN: The new item(s) to be added to the array.

    EDIT

    To remove an element from the array you can use the same splice function in the following way:

    array.splice(1, 1);
    alert(toString(array));
    

    Here we are indicating to splice at position 1, remove 1 element but since we are not providing any elements to insert, none is inserted, which results in a single element at position 1 removed.

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

Sidebar

Related Questions

I am curious about git add action, so I do some test. create a
Curious question: Take this function: function something():Array { var ar:Array = []; var i:MovieClip;
im curious about using schema export fully for my next project. I see it
I'm curious about adding references through the Property Pages dialog in Visual Studio C++.
I'm currently adding some photo-taking functionality to my application. I'm curious about something, though.
Just curious about the best way to go about this. I want to make
Just curious about the encodings that system is using when handling string storing(if it
I'm curious about people's approaches to using stored procedures in a database that is
Curious about running multiple xvfb displays: I have between 10-50 instances of a script
Being curious about how maven works exactly, I've checked out its source codes. Astonishingly,

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.