So I want to create a dynamic key in a js object. I know I can create it like this but id doesn't fullfil my requirements
return {
[dynamicKey]: value
}
But the problem is I want something like this
return {
[dynamicKey]_id: value
}
but it doesn't let me, I tried concatenating but it didn't work.
return {
[dynamicKey] + '_id': value
}
I've also searched online for a solution but couldn't find any.
you need to put your _id string inside of the square brackets or as part of the dinamicKey var.
return {
[dinamicKey + '_id']: value
}
dinamicKey += '_id';
return {
[dinamicKey]: value
}
Any expression in the square braces will be executed just like a normal expression. So how about this:
return {
[dinamicKey + '_id']: value
}