【问题标题】:How to push an array to an Object in JavaScript如何在 JavaScript 中将数组推送到对象
【发布时间】: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 服务器端点获取数组。

【问题讨论】:

  • JavaScript push to array的可能重复
  • 对象没有push 方法。您只需为他们分配一个新属性data.rows = rows
  • data.rows = whatever;

标签: javascript


【解决方案1】:

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

data.rows = this.state.rows;

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      你只需要调用下面的代码:

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

      【讨论】:

        【解决方案4】:

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

        在这种情况下,您可以这样做:data[rows] = this.state.rows

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-05
          • 1970-01-01
          • 2021-11-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多