I'm trying to find how to pass a variable as object index, for example:
function createObject(index,value){
var data = {index:value};
console.log(data);
}
createObject('hello','world');
//result Object { index="world"} instead of Object { "hello"="world"}
I couldn't find this specific answer so far..
Thanks!
You can use object[index] notation:
function createObject(index,value){
var data = {};
data[index] = value;
console.log(data);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With