1

I want to use this function

function setPlayerInternalData(playerid, key, value) {
    server.UpdateUserInternalData({
        playfabid: playerid,
        data: {
            key: value
        }
    });
}

where key is set by any string I submit to it. it works for value but idk how to make it work for key.

Thanks for your help !

Dream
  • 21
  • 3
  • 4
    use `[key]: value`, Read about [computed property names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names) – Shidersz May 02 '19 at 18:23
  • thanks for your help – Dream May 02 '19 at 18:24
  • Possible duplicate of [How to use a variable for a key in a JavaScript object literal?](https://stackoverflow.com/questions/2274242/how-to-use-a-variable-for-a-key-in-a-javascript-object-literal) – Wendelin May 02 '19 at 18:24

2 Answers2

1

use [] to wrap the key.

let myKey = 'three';
let myValue = '4';

let myObj = {
  [myKey]: myValue
};

console.log(myObj);
junvar
  • 11,151
  • 2
  • 30
  • 46
0

Just change

Note: I had answered the similar problem at https://stackoverflow.com/a/53123018/6615163 and this is also useful.

...
data: {
    key: value
}

to

...
data: {
    [key]: value
}

It will work.

For more info, check below.

> language = "JavaScript"
'JavaScript'
> 
> o = {language}
{ language: 'JavaScript' }
> 
> o2 = {language: language}
{ language: 'JavaScript' }
> 
> // Let's fix
undefined
> 
> o3 = {[language]: language}
{ JavaScript: 'JavaScript' }
> 
hygull
  • 8,464
  • 2
  • 43
  • 52