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

100
Views
Add a Key/Value pair to all Objects in Array from ajax success

I have problem with adding new keys and values in existing array. I get data with ajax and that result i need to put in existing array.

What i try:

  • forEach()
  • map()

Initial array:

var data = [
    {
        id: 0,
        text: 'enhancement'
    },
    {
        id: 1,
        text: 'bug'
    },
    {
        id: 2,
        text: 'duplicate'
    },
    {
        id: 3,
        text: 'invalid'
    },
    {
        id: 4,
        text: 'wontfix'
    }
];

In this array i need to add/push more results from the database.

See code:

 $.ajax({
            url: '/admin/gradovi/'+country_id,  
            dataType: 'json',   
            success: function(result) {             

                // loop all json result 

                jQuery.each(result, function(index, item) {

                    $.each(result[index], function(k, v) {

                       // mdata.map(v => ({id: v.id, text: v.text})) // not work

                         mdata.forEach(obj => {obj.id = v.id; return obj;}); // aslo not work

                        console.log(mdata);
                    });

                 });                  
            }
        });

Here is ajax json

{
  "results": [
    {
      "id": 1,
      "text": "Option 1"
    },
    {
      "id": 2,
      "text": "Option 2"
    }
  ], 
}

Debug for mdata.map(v => ({id: v.id, text: v.text}))

enter image description here

about 4 years ago · Juan Pablo Isaza
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!