I am trying to push an array to an object in JavaScript.
This is how my object looks like (before pushing):
const data = {
columns: [
{
label: "InvoiceID",
field: "InvoiceID",
sort: "asc",
width: 150
},
{
label: "Vendor Name",
field: "Vendor Name",
sort: "asc",
width: 270
},
{
label: "Invoice Date",
field: "Invoice Date",
sort: "asc",
width: 200
}
]
};
So I want to add (push) another array called rows so it will look like something below:
const data = {
columns: [
{
label: "InvoiceID",
field: "InvoiceID",
sort: "asc",
width: 150
},
{
label: "Vendor Name",
field: "Vendor Name",
sort: "asc",
width: 270
},
{
label: "Invoice Date",
field: "Invoice Date",
sort: "asc",
width: 200
}
],
rows: [
{
name: "Tiger Nixon",
position: "System Architect",
office: "Edinburgh",
age: "61",
date: "2011/04/25",
salary: "$320"
},
{
name: "Garrett Winters",
position: "Accountant",
office: "Tokyo",
age: "63",
date: "2011/07/25",
salary: "$170"
}
]
};
This is what I have been trying and it is not working.
const rows = this.state.rows;
data.push(rows);
In case you wonder, I am getting this rows from my database using axio There is no error in the array as it looks exactly as shown above. Problem is I can't push the array to data object. How can I achieve this?
Don't make this an duplicate since I am getting the array from my NodeJS server endpoint.
pushmethod. You just assign a new property to themdata.rows = rows.data.rows = whatever;