I have tried the following code. students consists of array of objects. When I add a new array of objects inside students it should be displayed in a tabular format.
var newArraypush = [{
Id: 1010,
Name : "herfr",
Course : "Aeronautical Engineering",
Age : 65,
Gender: "Male"
},
{
Id: 1011,
Name : "rfrsfd",
Course : "ISIS",
Age : 40,
Gender: "Male"
}];
this.students.push(newArraypush);
Actual output:-
0: {Id: 1005, Name: "ggtge", Course: "M.Phil", Age: 34, Gender: "Male"}
1: Array(2)
0: {Id: 1010, Name: "herfr", Course: "Aeronautical Engineering",
Age: 65, Gender: "Male"}
1: {Id: 1011, Name: "rfrsfd", Course: "IIT", Age: 50, Gender:
"Male"
Expected output:- console.log(students);
0: {Id: 1005, Name: "ggtge", Course: "M.Phil", Age: 34, Gender: "Male"}
1: {Id: 1010, Name: "herfr", Course: "Aeronautical Engineering",
Age: 65, Gender: "Male"}
2: {Id: 1011, Name: "rfrsfd", Course: "IIT", Age: 50, Gender:
"Male"
Array.push is just add a new element to your array. In your example you are trying to merge two array. Based on the definition Array.push can not do it directly. There are many ways to merge arrays in es6.
First one is using Array.push but you must give elements of your array as a parameter.
this.students.push([...newArraypush]);
The second way is using concat
this.students = this.students.concat(newArraypush);
The last one is like that
this.students = [...this.students, ...newArraypush];
This method is declared in ES6 and to understand this type you need to know the following
As we know, this.students means array of any based on your example
If we use [...this.students] this means objects of students array.
Then, in third way we are creating a new array which contains elements of studens array and elements of newArraypush and equalize it to the students array.
For example:
const a = [1,2,3];
const b = [4,5,6];
const c = [...a, ...b];
console.log(c) -> [1,2,3,4,5,6]
const d = [...a, ...b, 7,8,9,10];
console.log(d) -> [1,2,3,4,5,6,7,8,9,10];