1

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.

3
  • Possible duplicate of JavaScript push to array Commented Oct 5, 2019 at 13:00
  • 3
    Objects do not have a push method. You just assign a new property to them data.rows = rows. Commented Oct 5, 2019 at 13:01
  • data.rows = whatever; Commented Oct 5, 2019 at 13:01

4 Answers 4

0

JavaScript is a dynamic language. You can add fields to an object at runtime. Just add a field to the object:

data.rows = this.state.rows;
Sign up to request clarification or add additional context in comments.

Comments

0

There's no such push function in Object like Array.

But you can do that like below:

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

Comments

0

JavaScript objects do not have .push() method. only array has .push() method. If you want to add another key-value pair inside an object during run-time, you could do something like: objectName[key] = value;

In this scenario you can do : data[rows] = this.state.rows

Comments

0

you just need to call bellow code:

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

And then your Object will look like you want like below:

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

Description: Everytime you need to push(add) a key in your object, you should use this syntaxt instead of push. because the object it's not an array.

for Example:

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

obj["your_new_key"]=whatever;

console.log(obj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.