How to push an array to an Object in JavaScript

javascript push object into array with key
javascript push object into array at index
javascript push array into array
javascript array push key value pair
javascript array of objects
javascript add to object
push multiple items to array javascript
javascript object

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.


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;

Javascript Object push() function, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  The push () method adds new items to the end of an array, and returns the new length. Note: The new item (s) will be added at the end of the array. Note: This method changes the length of the array. Tip: To add items at the beginning of an array, use the unshift () method.


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

Create an Array of Objects, If you want cart to be an array of objects in the form { element: { id: 10, quantity: 1} } then The native JavaScript object has no push() method. Adding elements to an array. The following code creates the sports array containing two elements, then appends two elements to it. The total variable contains the new length of the array. let sports = ['soccer', 'baseball'] let total = sports.push('football', 'swimming') console.log(sports) // ['soccer', 'baseball', 'football', 'swimming'] console.log(total) // 4.


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

Array.prototype.push(), Get code examples like "array push object javascript" instantly right from your google search results with the Grepper Chrome Extension. So, we are going to push an object (maybe empty object) into that array. myArray.push({}), or myArray.push({""}). This will push an empty object into myArray which will have an index number 0, so your exact object is now myArray[0] Then push property and value into that like this: myArray[0].property = value; //in your case: myArray[0]["01"] = "value";


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

JavaScript Array push() Method, JavaScript push method adds one or more elements at the end of an array and returns the new length of the array. Object.assign () is the first method we'll cover for converting an array to an object. This method is used to copy values from one or more source objects to a new object. The function takes two parameters: The target object you wish to modify (this will be returned by the function)


JavaScript: Push into an Array inside an Object?, The push method adds values to the end of an array, and the pop method does the opposite, removing the last value in the array and returning it. You can use the Object.keys method to get an array of the keys, then use the Array#map method to return a new array containing individual objects for each property. This ES6 one-liner should do it: const splitObject = o => Object.keys(o).map(e => ({ [e]: o[e] })); Or in ES5:


Adding elements to object, Javascript array push() an inbuilt function that adds a new item to the end of Arrays in JS are list-like objects whose prototype has methods to  You need to have the 'data' array outside of the loop, otherwise it will get reset in every loop and also you can directly push the json. Find the solution below:-


array push object javascript Code Example, const convertArrayToObject = (array, key) => {};. We will then reduce the array, and create a unique property for each item based on the key we  You’ll learn how to push JSON object into an array using JavaScript. You can make use of Array.push method to push a JSON object to an array list. let list = []; let myJson = { "name" : "sam" } list.push( myJson); console.log( list) Copy.