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:
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}))