1//You need to make the object first, then use [] to set it.
2
3var key = "happyCount";
4var obj = {};
5obj[key] = someValueArray;
6myArray.push(obj);
1//For ES6 and Babel
2{
3 [yourKeyVariable]: "yourValue",
4}
5
6// ES5 Alternative
7// Create the object first, then use [] to set your variable as a key
8var yourObject = {};
9
10yourObject[yourKeyVariable] = "yourValue";
1var fighters = new WeakMap();
2var bruce = {name: 'Bruce Lee'};
3var chuck = {name: 'Chuck Norris'};
4
5fighters.set(bruce, 'Jeet Kune Do');
6fighters.set(chuck, 'Karate');
7
8console.log(fighters.get(bruce)); // Jeet Kune Do
9console.log(fighters.get(chuck)); // Karate
1let yourKeyVariable = "Fire";
2{
3 [yourKeyVariable]: someValue,
4}
5
6/* object will resolve to { Fire : someValue } */
7
8/*
9 This is similar to adding k-v pair to an object using the other syntax
10ie. object[yourKeyVariable] = someValue;
11*/
1var key = "happyCount";
2var obj = {};
3obj[key] = someValueArray;
4myArray.push(obj);
5