How to create an object with dynamic keys in JavaScript?

To create an object with dynamic keys in JavaScript, you can use ES6’s computed property names feature.
The computed property names feature allows us to assign an expression as the property name to an object within object literal notation.
Here is an e…


This content originally appeared on DEV Community and was authored by Muhammad Bilal Mohib-ul-Nabi

To create an object with dynamic keys in JavaScript, you can use ES6's computed property names feature.
The computed property names feature allows us to assign an expression as the property name to an object within object literal notation.
Here is an example:

const key = 'title';
const value = 'JavaScript';

const course = {
[key]: value,
price: '$99'
};

console.log(course.title); // JavaScript
console.log(course.price); // $99

The value of the key can be any expression as long as it is wrapped in brackets []:

const key = 'title';
const value = 'JavaScript';

const course = {
    [key + '2']: value,
    price: '$99'
};

console.log(course.title2);  // JavaScript
console.log(course.price);  // $99 

Copied From
https://attacomsian.com/blog/javascript-create-object-with-dynamic-keys#


This content originally appeared on DEV Community and was authored by Muhammad Bilal Mohib-ul-Nabi


Print Share Comment Cite Upload Translate Updates
APA

Muhammad Bilal Mohib-ul-Nabi | Sciencx (2021-12-14T05:31:41+00:00) How to create an object with dynamic keys in JavaScript?. Retrieved from https://www.scien.cx/2021/12/14/how-to-create-an-object-with-dynamic-keys-in-javascript/

MLA
" » How to create an object with dynamic keys in JavaScript?." Muhammad Bilal Mohib-ul-Nabi | Sciencx - Tuesday December 14, 2021, https://www.scien.cx/2021/12/14/how-to-create-an-object-with-dynamic-keys-in-javascript/
HARVARD
Muhammad Bilal Mohib-ul-Nabi | Sciencx Tuesday December 14, 2021 » How to create an object with dynamic keys in JavaScript?., viewed ,<https://www.scien.cx/2021/12/14/how-to-create-an-object-with-dynamic-keys-in-javascript/>
VANCOUVER
Muhammad Bilal Mohib-ul-Nabi | Sciencx - » How to create an object with dynamic keys in JavaScript?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/14/how-to-create-an-object-with-dynamic-keys-in-javascript/
CHICAGO
" » How to create an object with dynamic keys in JavaScript?." Muhammad Bilal Mohib-ul-Nabi | Sciencx - Accessed . https://www.scien.cx/2021/12/14/how-to-create-an-object-with-dynamic-keys-in-javascript/
IEEE
" » How to create an object with dynamic keys in JavaScript?." Muhammad Bilal Mohib-ul-Nabi | Sciencx [Online]. Available: https://www.scien.cx/2021/12/14/how-to-create-an-object-with-dynamic-keys-in-javascript/. [Accessed: ]
rf:citation
» How to create an object with dynamic keys in JavaScript? | Muhammad Bilal Mohib-ul-Nabi | Sciencx | https://www.scien.cx/2021/12/14/how-to-create-an-object-with-dynamic-keys-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.