Add a Property to a JavaScript Object Using a Variable as the Name

How to create an object property from a variable value in JavaScript?

There's the dot notation and the bracket notation

myObj[a] = b;

Add a property to a JavaScript object using a variable as the name?

You can use this equivalent syntax:

obj[name] = value

Example:

let obj = {};
obj["the_key"] = "the_value";

or with ES6 features:

let key = "the_key";
let obj = {
[key]: "the_value",
};

in both examples, console.log(obj) will return: { the_key: 'the_value' }

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.

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;

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;
}

Is it possible to add dynamically named properties to JavaScript object?

Yes.

var data = {

'PropertyA': 1,

'PropertyB': 2,

'PropertyC': 3

};

data["PropertyD"] = 4;

// dialog box with 4 in it

alert(data.PropertyD);

alert(data["PropertyD"]);

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,
}

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
}
};
}

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
};


Related Topics



Leave a reply



Submit