Array push with key javascript

Objects and Arrays are simple to understand data structures. However, everyday is a new learning day. Today, came across this situation where I wanted to use the push method on array to push key, value pair. The solution was very interesting and worth documenting.


// Below is the desired JSON output that we wanted to have.

"desiredArray" : [
  "ourKey" : "ourValue"
];

  • Create an empty object
  • Use this object to insert your key and value
  • Push the object into the desired array


var tempObj = {};
tempObj[ourKey] = ourValue;
desiredArray.push(obj);

Push an Object to an Array in JavaScript #

To push an object into an array, call the push() method, passing it the object as a parameter. For example, arr.push({name: 'Tom'}) pushes the object into the array. The push method adds one or more elements to the end of the array.

Copied!

let arr = []; const obj = {name: 'Tom'}; arr.push(obj); console.log(arr); // 👉️ [{name: 'Tom'}]

We used the Array.push method to push an object into an array.

The object gets pushed to the end of the array.

If you only have the values that the object should contain, create the object before pushing it into the array.

Copied!

let arr = []; const obj = {}; const name = 'Tom'; obj['name'] = name; arr.push(obj); console.log(arr); // 👉️ [{name: 'Tom'}]

We can use bracket notation to add one or more key-value pairs to the object.

Once the key-value pairs are assigned to the object, use the push method to add the object to the end of the array.

The same approach can be used to push multiple objects to an array.

Copied!

const arr = []; const obj1 = {name: 'Alice'}; const obj2 = {name: 'Bob'}; const obj3 = {name: 'Carl'}; arr.push(obj1, obj2, obj3); // 👇️ [{name: 'Alice'}, {name: 'Bob'}, {name: 'Carl'}] console.log(arr);

The Array.push() method takes one or more values and pushes them to the array.

This enables us to pass multiple, comma-separated objects as arguments in the call to push().

Further Reading #

  • Get an Object's Key by its Value using JavaScript
  • How to get the Length of an Object in JavaScript
  • Get Difference between two Arrays of Objects in JavaScript
  • How to Initialize an Array of Objects in JavaScript
  • Convert an Object's Values to Comma-Separated String in JS
  • Convert an Array of Objects to Array of Values in JS

Examples

Add a new item to an array:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");

Try it Yourself »

Add two new items to the array:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi", "Lemon");

Try it Yourself »


Definition and Usage

The push() method adds new items to the end of an array.

The push() method changes the length of the array.

The push() method returns the new length.


Syntax

array.push(item1, item2, ..., itemX)

Parameters

Parameters Description
item1
item2
..
itemX
The item(s) to add to the array.
Minimum one item is required.

Return Value

Type Description
A number The new length of the array.


More Examples

Add 3 items to the array:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi", "Lemon", "Pineapple");

Try it Yourself »

push() returns the new length:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");

Try it Yourself »


Browser Support

push is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes

How do you push an object with a key in an array?

To push an object into an array, call the push() method, passing it the object as a parameter. For example, arr. push({name: 'Tom'}) pushes the object into the array. The push method adds one or more elements to the end of the array.

How do you push a key and value in an array?

Answer: Use the Square Bracket [] Syntax php // Sample array $array = array("a" => "Apple", "b" => "Ball", "c" => "Cat"); // Adding key-value pairs to an array $array["d"] = "Dog"; $array["e"] = "Elephant"; print_r($array); ?>

How can I add a key

To add a key/value pair to all objects in an array: On each iteration, use dot notation to add a key/value pair to the current object. The key/value pair will get added to all objects in the array.

What does array push () do?

Array.prototype.push() The push() method adds one or more elements to the end of an array and returns the new length of the array.