You have to create an object. Assign the values to the object. Then push it into the array:
var nietos = [];
var obj = {};
obj["01"] = nieto.label;
obj["02"] = nieto.value;
nietos.push(obj);
Answer from user3589620 on Stack OverflowYou have to create an object. Assign the values to the object. Then push it into the array:
var nietos = [];
var obj = {};
obj["01"] = nieto.label;
obj["02"] = nieto.value;
nietos.push(obj);
Create an array of object like this:
var nietos = [];
nietos.push({"01": nieto.label, "02": nieto.value});
return nietos;
First you create the object inside of the push method and then return the newly created array.
javascript - push object array into array by index - Stack Overflow
javascript - How can I insert an item into an array at a specific index? - Stack Overflow
How to push Array in to nested Object? I cant use .push() since im trying to add a new key value pair? Ive tried looking at stack overflow and other places but no luck. if anyone can point me in the right direction would be super helpful please :)
Push an array within the object of an array at specific index in javascript - Stack Overflow
You want the splice function on the native array object.
arr.splice(index, 0, item); will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert).
In this example we will create an array and add an element to it into index 2:
var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";
console.log(arr.join()); // Jani,Hege,Stale,Kai Jim,Borge
arr.splice(2, 0, "Lene");
console.log(arr.join()); // Jani,Hege,Lene,Stale,Kai Jim,Borge
Run code snippetEdit code snippet Hide Results Copy to answer Expand
UPDATE (24 May 2024)
You can now use the toSpliced method which behaves just like splice, however it returns a new array without mutating the existing one.
You could update the previous example like so:
const updated = arr.toSpliced(2, 0, "Lene");
You can implement the Array.insert method by doing this:
Array.prototype.insert = function ( index, ...items ) {
this.splice( index, 0, ...items );
};
Then you can use it like:
var arr = [ 'A', 'B', 'E' ];
arr.insert(2, 'C', 'D');
// => arr == [ 'A', 'B', 'C', 'D', 'E' ]
Object is a reference type, so you can get the target object from the array by index and then update its newArr property.
let arr = [{
name: {
"id": 1
},
data: {
"dataVal": 'value1'
}
}, {
name: {
"id": 2
},
data: {
"dataVal": 'value2'
}
}, {
name: {
"id": 3
},
data: {
"dataVal": 'value3'
}
}]
const update = (array, index, property, value) => {
array[index][property] = value;
}
update(arr, 0, "newArr", [])
console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Try this simple utility function.
const newArr = [1, 2, 3]
const arr = [{
name: {
"id": 1
},
data: {
"dataVal": "value1"
}
}, {
name: {
"id": 2
},
data: {
"dataVal": "value2"
}
},
{
name: {
"id": 3
},
data: {
"dataVal": "value3"
}
}
]
function insertArr(ar, i, newAr) {
ar[i] = { ...ar[i],
newAr
}
return ar
}
console.log(insertArr(arr, 1, newArr))
Run code snippetEdit code snippet Hide Results Copy to answer Expand