Answers for "javascript set object key by variable"

10

variable key name js

//You need to make the object first, then use [] to set it.

var key = "happyCount";
var obj = {};
obj[key] = someValueArray;
myArray.push(obj);
Posted by: Guest on June-19-2020
1

javascript set object key by variable

/* FOR ES6 and Babel */
{
    [yourKeyVariable]: someValueArray,
}  

//for older versions

var key = "happyCount";
var obj = {};
obj[key] = someValueArray;
myArray.push(obj);
Posted by: Guest on June-07-2021
8

javascript create object key from variable

//For ES6 and Babel
{
    [yourKeyVariable]: "yourValue",
}

// ES5 Alternative
// Create the object first, then use [] to set your variable as a key
var yourObject = {};

yourObject[yourKeyVariable] = "yourValue";
Posted by: Guest on September-28-2020
-2

js change key value in object

var object = { boo:true, baa:5 };
console.log(object);
function change() {
  object.boo = false;
  object.baa++;
};
change();
console.log(object);
//Hope this helps!
Posted by: Guest on June-21-2020

Code answers related to "javascript set object key by variable"

Code answers related to "Javascript"

Browse Popular Code Answers by Language