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 - Apply Object.assign() to add multiple properties at once. ... let person = { name: 'John' }; Object.assign(person, { age: 30, city: 'New York' }); console.log(person); Explain Code · This demonstrates how to use Object.assign() to add more ...
Discussions

Add multiple key value pairs in a JavaScript object - Stack Overflow
I want a JavaScript object which should have multiple key values pairs. However, I am unable to add. On clicking a function, I am getting multiple key value pairs as {id:"1" ,desc: "first"} , {id... More on stackoverflow.com
🌐 stackoverflow.com
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
Add a new key-value pair to a JavaScript object
I want to insert a new key-value pair into an existing JavaScript object. How can I achieve that? For example, if I have this object: let book = {title: ‘1984’, author: ‘George Orwell’}; How can I add a ‘year’ property? More on community.latenode.com
🌐 community.latenode.com
0
0
October 21, 2024
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
People also ask

How can I create a dynamic key in JavaScript?
A: You can create a dynamic key in JavaScript using bracket notation, such as dictionary[dynamicKey] = "value";.
🌐
sqlpey.com
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
Can I use arrays as keys in JavaScript?
A: No, only strings can be used as keys in JavaScript objects. However, you can convert an array to a string or use objects directly for more complex key requirements.
🌐
sqlpey.com
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
What is the difference between dot notation and bracket notation?
A: Dot notation is used for fixed key names, while bracket notation can accommodate dynamic keys, spaces, and special characters.
🌐
sqlpey.com
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
🌐
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.
🌐
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 - Use Object Spread Operator: Employ the object spread operator to add the new key and value seamlessly. const myObject = { existingKey: 'existingValue' }; const newObject = { ...myObject, newKey: 'newValue' }; By incorporating the object spread ...
🌐
Stack Overflow
stackoverflow.com › questions › 49335930 › add-multiple-key-value-pairs-in-a-javascript-object
Add multiple key value pairs in a JavaScript object - Stack Overflow
... prdObj = [{id:"1" ,desc: "first"} , {id:"2", desc:"two"} , {id:"3" , desc:"three"}]; //when adding prdObj.push( {id:"4" , desc:"four"}); ... var yourObject = [{"key1":"yourvalue", "key2":"yourvalue"}, {"key1":"yourvalue", "key2":"yourvalue"}];
Find elsewhere
🌐
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 - Similar to dot notation, square bracket notation comes in handy when dealing with dynamic keys, but can also work with static keys. Apart from that, it's exactly the same as dot notation both in functionality and performance. const obj = { a: 1 }; const bKey = 'b'; obj[bKey] = 2; obj['c'] = 3; // obj = { a: 1, b: 2, c: 3 } Object.assign() is slightly different than the previous two options. It can be used to add multiple properties to an object at once and it can also shallow merge two or more objects.
🌐
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 - <script> const getId = () => { //API call and other stuff return "ID" } const objA = { codez: 'up', test: '123', } console.log(objA) const key3 = getId() Object.defineProperty(objA, key3, { value: 'Nothing', writable: true, enumerable: true, configurable: true }) console.log(objA) </script>
🌐
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 ... deal with object key pair dynamics. A: You can create a dynamic key in JavaScript using bracket notation, such as dictionary[dynamicKey] = "value";....
🌐
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!
A: Yes, you can add multiple key-value pairs simultaneously using Object.assign(targetObject, sourceObject1, sourceObject2) or with the spread syntax: { ...existingObject, ...newPropertiesObject1, ...newPropertiesObject2 }. Both methods allow you to effectively js add key value to object from ...
🌐
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.
🌐
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_append_key_value_to_object.php
js append key value to object: Master Dynamic Data Handling & Unlock Powerful JavaScript Patterns
JavaScript objects are dynamic and allow for easy runtime modification by appending new key-value pairs. The primary methods for js append key value to object are dot notation (for static, valid identifiers) and bracket notation (for dynamic or complex keys).
🌐
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.
🌐
Latenode
community.latenode.com › other questions › javascript
Add a new key-value pair to a JavaScript object - JavaScript - Latenode Official Community
October 21, 2024 - I want to insert a new key-value pair into an existing JavaScript object. How can I achieve that? For example, if I have this object: let book = {title: ‘1984’, author: ‘George Orwell’}; How can I add a ‘year’ property…
🌐
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
You can create a dictionary dynamically by defining an empty object and then adding key-value pairs using various methods. This article covers multiple approaches for dynamically creating and modifying dictionaries in JavaScript.
Published: September 16, 2024
🌐
Abin John
abinjohn.hashnode.dev › dynamic-object-keys
Dynamic Object Keys in JavaScript
September 3, 2022 - So even though we intended keys.forEach((key, i) => object.key = values[i]) to create multiple key-value pairs with object.key referring to a new key for each iteration, object.key always accesses the same property key in the object.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-add-key-value-pair-to-object
How to Add a Key/Value pair to an Object in JavaScript | bobbyhadz
Use the Object.assign() method, passing it a target and source objects as arguments. Here is an example of adding key-value pairs to an object using dot .