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

时间:2019-10-05 12:59:38

标签: javascript

我正在尝试将数组推入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
    }
  ]
};

所以我想添加(推入)另一个名为的数组,因此它将类似于以下内容:

  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);

以防万一,我正在使用rows从数据库中获取此axio,因为数组看起来与上面的显示完全一样,所以没有错误。问题是我无法将数组推送到data对象。我该如何实现?

请勿重复此操作,因为我是从NodeJS服务器端点获取数组的。

4 个答案:

答案 0 :(得分:0)

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

data.rows = this.state.rows;

答案 1 :(得分:0)

对象(如Array)中没有这样的push函数。

但是您可以按照以下步骤进行操作:

const objectValue = {
  array1: [
    {
      id: 1,
      value: 'one',
    },
    {
      id: 2,
      value: 'two',
    }
  ]
};

const newArray = [
    {
      id: 3,
      value: 'three',
    },
    {
      id: 4,
      value: 'four',
    }
  ];
objectValue.array2 = newArray;
console.log(objectValue);

答案 2 :(得分:0)

您只需要调用以下代码:

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

描述:每当您需要在对象中推送(添加)键时,都应使用此语法而不是push。因为对象不是数组。

例如:

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

obj["your_new_key"]=whatever;

console.log(obj);

答案 3 :(得分:0)

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

在这种情况下,您可以执行以下操作:data [rows] = this.state.rows