Tengo un problema al agregar nuevas claves y valores en la matriz existente. Obtengo datos con ajax y ese resultado necesito ponerlo en una matriz existente.
Lo que intento:
matriz inicial:
var data = [ { id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' } ];En esta matriz, necesito agregar/empujar más resultados de la base de datos.
Ver código:
$.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); }); }); } });Aquí está ajax json
{ "results": [ { "id": 1, "text": "Option 1" }, { "id": 2, "text": "Option 2" } ], } Depurar para mdata.map(v => ({id: v.id, text: v.text}))