[Vuejs]-TypeScript: How to add a property to a class with a string as the identifier

1👍

Just create a new key in the object with the value you want

let row = {};
stringArr.forEach(player => {
    row[player] = 100;
});
👤smac89

1👍

You could create the object using JavaScript’s bracket notation:

let stringArr = ['player1score', 'player2score', 'player3score'];
let row = {};
stringArr.forEach(player => row[player] = 100);
// row = {player1score: 100, player2score: 100, player3score: 100}

See MDN for more details.

1👍

In modern browser you can do Object.fromEntries()

let stringArr = ['player1score', 'player2score', 'player3score'];

let obj = Object.fromEntries(stringArr.map(val => [val, 100]))

console.log(obj)

Leave a comment