Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Adding to a Multidimensional Array in Javascript

I have a multidimensional array that I am creating from a JSON Ajax call to my DB.

With in my Web App, I am trying to dynamically add a new row to the array using javascript along the lines of:

list[new_row_id].item_id = new_value ;
list[new_id].item_title = new_title  ;

Clearly I am doing something wrong.

Any guidance would be appreciated.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Adding an element to the end of an array is done with the push-function:

array.push({"item_id": 10101, "item_title": "new title"});
about 4 years ago · Juan Pablo Isaza Report

0

If you meant "new_id" and "new_row_id" to be the same and it differs from an index starting from 0, because the keys are individual database keys, then you can solve it that way:

var any_db_id = 33;
var new_title = 'new title';
list[any_db_id] = {
    item_id: any_db_id,
    title: new_title
}

That will result in a JSON looking like that:

{
    1: {
        item_id: 1,
        title: 'Entry coming from database 1'
    },
    5: {
        item_id: 5,
        title: 'Entry coming from database 2'
    },
    33: {
        item_id: 33,
        title: 'new title'
    }
}

If the new_id and new_row_id are still same but it is actually just an index (each item has 0, 1, 2 and so on as key), then you should use the solution of Virendra Katariya.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!