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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:41:44+00:00 2026-05-25T18:41:44+00:00

How do I use array.push to add an object with a custom index in

  • 0

How do I use array.push to add an object with a custom index in an array?

I have this script but it doesnt work. Chrome’s Javascript console is not outputting errors

var tempList = new Array();
$('#add').click(function(){
  var split = $('#itemid').val().split('_');
  var itemid = split['0'];
  var lbs = $('#lbs').val();
  tempList.push(tempList['itemid'] = lbs);
  for (var i; i<=(tempList.length - 1); i++){
    if (i==0){
      var list = tempList[i]+ '<br />';
    } 
    else{
      var list =  list + tempList[i]+ '<br />';
    }
  }
  alert(list);
});
  • 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-25T18:41:45+00:00Added an answer on May 25, 2026 at 6:41 pm

    Old answer: Your script may not work, because you have to initialise the index of the loop: var i=0;


    Question (new, at the comment chain):

    Hi, hope you dont mind another question but how do I display the
    content of tempList[] in a table?

    Answer:
    See the code below. After the loop, I have included several ways to append the data/table to the body, because you didn’t specify which method you wanted.

    var tempList = {};
    $('#add').click(function(){
        var split = $('#itemid').val().split('_');
        var returnTable = "<thead><tr><th>ItemId</th><th>Lbs</th></tr></thead><tbody>";
        var itemid = split[0];
        if(/^\d+$/.test(itemid)) return; //itemId is not a valid number: return now.
    
        var lbs = $('#lbs').val();
        tempList[itemid] = lbs; //refer by value, removed quotes around `itemId`.
        for (var i in tempList){
            returnTable += "<tr><td>" + i + "</td><td>" + tempList[i] + '</td></tr>';
        }
        returnTable += "</tbody>";
    
    
        var tbl = document.createElement("table");
        tbl.innerHTML = returnTable;
    
        //Various methods to add the table to your document are shown below
        // For each example, I assume that that specific element exists
    
        //When the data should be APPENDED to an existing TABLE ("#existingTable")
        var tblTo = document.getElementById("existingTable").tBodies[0];
        for(var i=0, len=tbl.tBodies[0].rows.length; i<len; i++){
            tblTo.appendChild(tbl.tBodies[0].rows[0]);
            //Removes the row from the temporary table, appends row to tblTo
        }
    
        //When the new table should replace the previous table ("#prevTable"):
        var prevTbl = document.getElementById("prevTable");
        prevTbl.parentNode.replaceChild(tbl, prevTbl);
    
        //When the table should be APPENDED to a container ("#container")
        document.getElementById("output").appendChild(tbl);
    
        //When the new table should replace the contents of a container ("#container")
        var container = document.getElementById("container");
        container.innerHTML = "";
        container.appendChild(tbl);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to add objects to an array by using push. I have a
Let's say I have a JavaScript object: function a(){ var A = []; this.length
I have an array that doesn't use the 0 index. The array starts from
In Java, we can always use an array to store object reference. Then we
I have a situation, where I need to create a new JavaScript object that
I have to send a two-dimensional JavaScript Array to a PHP page. Indeed, I'm
I use this code to create arrays inside array. array_push($lists, $list); And then I
I have a javascript object that contains a few objects that contain associative arrays.
I was using a custom made object handling library (for exams, can't use things
I have a vector<some_struct&> array in my code, and whenever I want to add

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.