In ES6...

In ES6, you can use a destructuring assignment;

ary.push({[name]: val});

However, given this is ES6 syntax, the usual caveats apply; this will not work in some browsers (noticably, IE and Edge 13)... although Babel will transpile this for you.


Without ES6 (legacy browser support)...

You need to define an object and use square bracket notation to set the property;

var obj = {};

obj[name] = val;

ary.push(obj);

If you get the urge to read into it more, see this article on the differences between square bracket and dot notation.

🌐
Vultr Docs
docs.vultr.com › javascript › examples › add-keyvalue-pair-to-an-object
JavaScript Program to Add Key/Value Pair to an Object | Vultr Docs
November 8, 2024 - Initialize an object with some properties. Utilize bracket notation to add a new key/value pair, especially useful when the key name is stored in a variable or needs to be computed.
Discussions

How can I add a key/value pair to a JavaScript object? - Stack Overflow
Here is my object literal: var obj = {key1: value1, key2: value2}; How can I add field key3 with value3 to the object? More on stackoverflow.com
🌐 stackoverflow.com
javascript - React - how to add dynamic key/value pair to an object? - Stack Overflow
I am trying to add dynamic key/value pair to an initial object with the appendInput() function. More on stackoverflow.com
🌐 stackoverflow.com
April 14, 2017
How to add key and a dynamic value pair for each object in the array ?
map is your friend More on reddit.com
🌐 r/vuejs
7
1
January 25, 2021
How do I create a typed dynamic key-value store?
What if you don't know the value of "settings-1", and instead is a string given from input or calculated dynamically. Then this cannot work. More on reddit.com
🌐 r/typescript
6
2
February 22, 2023
🌐
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to add key/value pair in javascript object dynamically
How To Add Key/Value Pair In Javascript Object Dynamically
March 29, 2024 - Add Key/Value Pair: Use dot notation to assign a new key and its corresponding value. const myObject = {}; myObject.newKey = 'someValue'; Another method to dynamically add key/value pairs is by using square bracket notation.
🌐
EyeHunts
tutorial.eyehunts.com › home › how to add key/value pair in javascript object dynamically
How to add key/value pair in JavaScript object dynamically
July 22, 2022 - Using bracket notation or the defineProperty() method you can add key/value pair in JavaScript object dynamically
🌐
Zipy
zipy.ai › blog › how-can-i-add-a-key-value-pair-to-a-javascript-object
how can i add a key value pair to a javascript object
April 12, 2024 - One of the simplest ways to add a key-value pair to an object is using dot notation. This method involves directly specifying the key as a property of the object and assigning it a value.
Find elsewhere
🌐
EnableGeek
enablegeek.com › home › add key value pair to object javascript
Add Key Value Pair to Object Javascript - EnableGeek
March 22, 2024 - In summary, you can add dynamic key-value pairs to objects in JavaScript using bracket notation or the ES6 computed property syntax.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › object › add key-value pair to object
How can I add a key-value pair to a JavaScript object? - 30 seconds of code
July 21, 2022 - const obj = { a: 1 }; Object.assign(obj, { b: 2 }, { c: 3 }); // obj = { a: 1, b: 2, c: 3 } Another, less-common, way to add a key-value pair to an object is to use Object.defineProperty(). This is the lest performant way to add a key-value pair to an object, but it allows the new property to be precisely defined.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-add-a-key-value-pair-to-an-object-in-javascript.php
How to Add a Key/Value Pair to an Object in Javascript
// Sample object var myCar = { make: "Ferrari", model: "Portofino", year: 2018 }; // Sample variables var myKey = "fuel"; var myValue = "Petrol"; // Dynamically adding a key/value pair myCar[myKey] = myValue; console.log(myCar); See the tutorial on JavaScript Objects to learn more about creating and manipulating objects.
🌐
ProgrammingBasic
programmingbasic.com › home › javascript › how to add dynamic key to an object in javascript
How to add dynamic Key to an object in JavaScript | ProgrammingBasic
This is a very simple way to add a key to your JavaScript object. We have to create the key using a bracket and assign a value to it. ... As you can see we have added the key(color) with value (red) in our object (objCar).
🌐
Codez Up
codezup.com › home › 3 ways to add dynamic key to object in javascript
3 ways to Add Dynamic Key to Object in Javascript | Codez Up
September 7, 2021 - So, I am going to share two cases, one is when we want to convert an array of strings to an array of objects, and in the 2nd case, we convert the array to a single object with dynamic key creation at runtime using reduce() function.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-add-key-value-pair-to-a-javascript-object
How to add Key-Value pair to a JavaScript Object? - GeeksforGeeks
July 11, 2025 - We are passing the object name and the field that we want to use in the object. It assign those fields to the given object. Example: This example shows the implementation of the above approach.
🌐
xjavascript
xjavascript.com › blog › how-can-i-add-a-key-value-pair-to-a-javascript-object
How to Add a Key/Value Pair to a JavaScript Object: Object Literal Example — xjavascript.com
The Object.assign() method copies properties from one or more source objects to a target object. It returns the modified target object. You can use it to add new key-value pairs by passing the target object and a source object containing the new pair(s)....
🌐
Codegive
codegive.com › blog › js_add_key_value_to_object.php
Master js add Key Value to Object (2024): The Ultimate Guide to Dynamic Data in JavaScript!
For dynamic keys, you combine spread syntax with bracket notation [variableName]: value. While not an object in the traditional sense, Map provides an alternative for storing key-value pairs, especially when keys might not be strings, or when you need better performance for frequent ...
🌐
sqlpey
sqlpey.com › javascript › top-4-ways-to-dynamically-create-and-add-key-value-pairs-in-javascript
Top 4 Ways to Dynamically Create and Add Key-Value Pairs in JavaScript
November 23, 2024 - For applications needing more advanced ... key pair dynamics. A: You can create a dynamic key in JavaScript using bracket notation, such as dictionary[dynamicKey] = "value";....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-create-dictionary-and-add-key-value-pairs-dynamically
How to create dictionary and add key–value pairs dynamically? - GeeksforGeeks
A dictionary, also known as a map, is a collection of key-value pairs. You can create a dictionary dynamically by defining an empty object and then adding key-value pairs using various methods.
Published: September 16, 2024