如何在JavaScript中将数组推送到对象

如何在JavaScript中将数组推送到对象,javascript,Javascript,我试图将数组推送到JavaScript中的对象 这是我的对象的外观(推之前): 因此,我想添加(推送)另一个名为行的数组,使其看起来如下所示: const data = { columns: [ { label: "InvoiceID", field: "InvoiceID", sort: "asc", width: 150 }, { label: "Vendor Name", field:

我试图将数组推送到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: [
    {
      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
数组中没有错误,因为它看起来与上面所示完全相同。问题是我无法将数组推送到
数据
对象。我怎样才能做到这一点

不要将其复制,因为我正在从我的NodeJS服务器端点获取数组。

JavaScript是一种语言。可以在运行时向对象添加字段。只需向对象添加一个字段:

data.rows = this.state.rows;
JavaScript是一种语言。可以在运行时向对象添加字段。只需向对象添加一个字段:

data.rows = this.state.rows;

在类对象数组中没有这样的
push
函数

但你可以像下面这样做:

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 objectValue={
阵列1:[
{
id:1,
值:“一”,
},
{
id:2,
值:“2”,
}
]
};
常数newArray=[
{
id:3,
价值:'三',,
},
{
id:4,
值:“四”,
}
];
objectValue.array2=newArray;

log(objectValue)
在类似对象的数组中没有这样的
push
函数

但你可以像下面这样做:

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 objectValue={
阵列1:[
{
id:1,
值:“一”,
},
{
id:2,
值:“2”,
}
]
};
常数newArray=[
{
id:3,
价值:'三',,
},
{
id:4,
值:“四”,
}
];
objectValue.array2=newArray;

log(objectValue)您只需调用以下代码:

let 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
    }
  ]
};

data["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"
    }
  ];
然后您的对象将看起来像您想要的,如下所示:

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"
    }
  ]
};
描述:每次需要在对象中推(添加)键时,都应该使用此语法而不是推。因为对象不是数组

例如:

let obj = { key1:'it is key one','key2':['key2 array','key2 array']}

obj["your_new_key"]=whatever;

console.log(obj);

您只需调用以下代码:

let 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
    }
  ]
};

data["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"
    }
  ];
然后您的对象将看起来像您想要的,如下所示:

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"
    }
  ]
};
描述:每次需要在对象中推(添加)键时,都应该使用此语法而不是推。因为对象不是数组

例如:

let obj = { key1:'it is key one','key2':['key2 array','key2 array']}

obj["your_new_key"]=whatever;

console.log(obj);

JavaScript对象没有.push()方法。只有数组具有.push()方法。 如果要在运行时在对象中添加另一个键值对,可以执行以下操作: objectName[键]=值


在此场景中,您可以执行以下操作:data[rows]=this.state.rows

JavaScript对象没有.push()方法。只有数组具有.push()方法。 如果要在运行时在对象中添加另一个键值对,可以执行以下操作: objectName[键]=值


在此场景中,您可以执行以下操作:data[rows]=this.state.rows

对象的可能副本没有
推送方法。您只需为它们分配一个新属性
data.rows=rows
data.rows=which对象的可能副本没有
推送
方法。您只需为它们分配一个新属性
data.rows=rows
data.rows=which