In normal the object with key we know will be look like,
function myFunc() {
const newObj = {
'key': 'value'
}
return newObj;
}
console.log(myFunc());
In the above you can able to see that the 'key'
act as a known string.
Suppose in your app if you get that string in dynamic format then you can add square bracket around like []
and can assign value of that property as a key to an object.
For eg.., You are getting the key from an object like,
const data = {
prop:'dynamicKey'
};
And you need to assign the value of prop as key to an obj then you can use it like,
[data.prop]: 'value'
const data = {
prop:'dynamicKey'
};
function myFunc() {
const newObj = {
[data.prop]: 'value'
}
return newObj;
}
console.log(myFunc());