Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new elements to an array without overwriting the first one?

I'm trying to add new elements to an Array but it keeps overwriting it the first element, i'm not sure if i'm doing it correctly, but this is my code:

//name and age contains value from a NgModel 
this.data = [
    { "name": this.name, "age": this.age },
];

//Adds them to a new array 
this.nArray = [];
this.nArray.push(this.data);
like image 493
QuickAccount123 Avatar asked Oct 20 '25 10:10

QuickAccount123


1 Answers

Use concat when you want to merge two arrays.

const nArray = [];
const arr = nArray.concat(this.data);

You can now use the new merged array for your purpose

like image 97
anto004 Avatar answered Oct 22 '25 01:10

anto004



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!