Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda