【发布时间】:2020-02-03 12:11:56
【问题描述】:
我正在尝试将数组推送到 JavaScript 中的对象。
这是我的对象的样子(推送之前):
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 的数组,所以它看起来像下面这样:
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"
}
]
};
这是我一直在尝试的,但它不起作用。
const rows = this.state.rows;
data.push(rows);
如果您想知道,我使用 axio 从我的数据库中获取此 rows 数组中没有错误,因为它看起来完全如上所示。问题是我无法将数组推送到data 对象。我怎样才能做到这一点?
不要重复,因为我是从我的 NodeJS 服务器端点获取数组。
【问题讨论】:
-
对象没有
push方法。您只需为他们分配一个新属性data.rows = rows。 -
data.rows = whatever;
标签: javascript