JS-Tips-And-Tricks
JS-Tips-And-Tricks copied to clipboard
Add an object key assigment from variable (faster way)
We want to pass a key to an object that is assigned to variable.
Instead of doing
const key = "b";
const obj = {
a: 1
};
obj[key] = "value";
We can define the object key without obj[key]
const key = "b";
const obj = {
a: 1,
[key]: "value"
};