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

JavascriptPropertiesObject

Javascript Problem Overview


I want to add a new property to 'myObj', name it 'string1' and give it a value of 'string2', but when I do it it returns 'undefined:

var myObj = new Object;
var a = 'string1';
var b = 'string2';
myObj.a = b;

alert(myObj.string1); //Returns 'undefined'
alert(myObj.a); //Returns 'string2'

In other words: How do I create an object property and give it the name stored in the variable, but not the name of the variable itself?

Javascript Solutions


Solution 1 - Javascript

Solution 2 - Javascript

ES6 introduces computed property names, which allow you to do

var myObj = {[a]: b};

Note browser support is currently negligible.

Solution 3 - Javascript

Dot notation and the properties are equivalent. So you would accomplish like so:

var myObj = new Object;
var a = 'string1';
myObj[a] = 'whatever';
alert(myObj.string1)

(alerts "whatever")

Solution 4 - Javascript

Ecu, if you do myObj.a, then it looks for the property named a of myObj. If you do myObj[a] =b then it looks for the a.valueOf() property of myObj.

Solution 5 - Javascript

Oneliner:

obj = (function(attr, val){ var a = {}; a[attr]=val; return a; })('hash', 5);

Or:

attr = 'hash';
val = 5;
var obj = (obj={}, obj[attr]=val, obj);

Anything shorter?

Solution 6 - Javascript

You could just use this:

function createObject(propName, propValue){
	this[propName] = propValue;
}
var myObj1 = new createObject('string1','string2');

Anything you pass as the first parameter will be the property name, and the second parameter is the property value.

Solution 7 - Javascript

You cannot use a variable to access a property via dot notation, instead use the array notation.

var obj= {
     'name' : 'jroi'
};
var a = 'name';
alert(obj.a); //will not work
alert(obj[a]); //should work and alert jroi'

Solution 8 - Javascript

As $scope is an object, you can try with JavaScript by:

$scope['something'] = 'hey'

It is equal to:

$scope.something = 'hey'

I created a fiddle to test.

Solution 9 - Javascript

The following demonstrates an alternative approach for returning a key pair object using the form of (a, b). The first example uses the string 'key' as the property name, and 'val' as the value.

Example #1:

(function(o,a,b){return o[a]=b,o})({},'key','val');

Example: #2:

var obj = { foo: 'bar' };
(function(o,a,b){return o[a]=b,o})(obj,'key','val');

As shown in the second example, this can modify existing objects, too (if property is already defined in the object, value will be overwritten).

>Result #1: { key: 'val' } > >Result #2: { foo: 'bar', key: 'val' }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionecuView Question on Stackoverflow
Solution 1 - JavascriptphilfreoView Answer on Stackoverflow
Solution 2 - JavascriptOriolView Answer on Stackoverflow
Solution 3 - JavascriptcgpView Answer on Stackoverflow
Solution 4 - Javascriptuser286806View Answer on Stackoverflow
Solution 5 - Javascriptuser2846569View Answer on Stackoverflow
Solution 6 - JavascriptAaron GeorgeView Answer on Stackoverflow
Solution 7 - Javascriptjroi_webView Answer on Stackoverflow
Solution 8 - JavascriptTuanView Answer on Stackoverflow
Solution 9 - JavascriptTrentView Answer on Stackoverflow