How to create an object property from a variable value in JavaScript?
There's the dot notation and the bracket notation
myObj[a] = b;
How To Set A JS object property name from a variable
var jsonVariable = {};
for(var i=1; i < 3; i++) {
jsonVariable[i + 'name'] = 'name' + i;
}
JavaScript set object key by variable
You need to make the object first, then use []
to set it.
var key = "happyCount";
var obj = {};
obj[key] = someValueArray;
myArray.push(obj);
UPDATE 2021:
Computed property names feature was introduced in ECMAScript 2015 (ES6) that allows you to dynamically compute the names of the object properties in JavaScript object literal notation.
const yourKeyVariable = "happyCount";
const someValueArray= [...];
const obj = {
[yourKeyVariable]: someValueArray,
}
Using a variable for a property name
You're close, you just need to add square brackets around the key name:
var product_data = {
[option_code]: size_code,
quantity: "1",
product_id: product_code,
}
Otherwise, you can do the following:
var product_data = {
quantity: "1",
product_id: product_code,
}
product_data[option_code] = size_code;
Using Variable for Property Name of Object - Javascript
To set variables as key names you have to use bracket notation;
console.log(first); // returns 'name'
var obj = {};
obj[first] = value;
objArr[key] = obj; // no longer a problem
Sorry it's more verbose :(
Edit;
In ES6 you can now use computed-property-names;
const key = 'name';
const value = 'james';
const obj = {
[key]: value
};
Property name on object from variable
You are looking for computed properties, this is an ES6 feature and not specific to TypeScript.
export function objectFactory(prop: string) {
return {
[prop]: {
valid: false
}
};
}
create object using variables for property name
If you want to use a variable for a property name, you can use Computed Property Names. Place the variable name between square brackets:
var foo = "bar";
var ob = { [foo]: "something" }; // ob.bar === "something"
If you want Internet Explorer support you will need to use the ES5 approach (which you could get by writing modern syntax (as above) and then applying Babel):
Create the object first, and then add the property using square bracket notation.
var foo = "bar";
var ob = {};
ob[foo] = "something"; // === ob.bar = "something"
If you wanted to programatically create JSON, you would have to serialize the object to a string conforming to the JSON format. e.g. with the JSON.stringify
method.
Passing a variable as a property name
You can use bracket notation to set the property name of an object. See chrome.storage.local.set using a variable key name, chrome.storage
var value = "123";
var name = "abc";
var obj = {};
obj[name] = value;
chrome.storage.local.set(obj);
Related Topics
Array.Length Is Zero, But the Array Has Elements in It
(Deep) Copying an Array Using Jquery
Getting "Typeerror: Failed to Fetch" When the Request Hasn't Actually Failed
About Closure, Lexicalenvironment and Gc
Difference Between Dom Parentnode and Parentelement
Parsing Url Hash/Fragment Identifier with JavaScript
Pass Arguments with Page.Evaluate
Can't Get Correct Return Value from an Jquery Ajax Call
Require Is Not Defined? Node.Js
Is <Boolean Expression> && Statement() the Same as If(<Boolean Expression>) Statement()
Retrieving Binary File Content Using JavaScript, Base64 Encode It and Reverse-Decode It Using Python
How to Add/Subtract Dates with JavaScript
JavaScript Can't Access Private Properties
Vuejs 2.0 Emit Event from Grand Child to His Grand Parent Component
Short-Polling VS Long-Polling for Real Time Web Applications
How to Perform an Http File Upload Using Express on Cloud Functions for Firebase (Multer, Busboy)