Create javascript object using constants with dots as property [duplicate] - javascript

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 8 years ago.
I have an object holding some of my program constants so that I can use it in all of the source code files. The constants object is something like this:
CONSTANTS = {
THING_TYPE: 'type',
THING_INFORMATION: 'information',
THING_DESCRIPTION: 'description',
THING_NAME: 'name',
manyOtherConstants
}
And I want to create objects using a similar notation and using the value of the constants as a property of the object; this is what I'm trying to do:
var myObject = {
CONSTANTS.THING_TYPE: 'whateverType',
CONSTANTS.THING_INFORMATION: {
CONSTANTS.THING_DESCRIPTION: 'whateverDescription',
CONSTANTS.THING_NAME: 'whateverName',
}
}
The problem is that I cannot use the constants in that way. Javascript says:
'SyntaxError: missing : after property id'
Is there any way of doing what I am trying to do using that notation? Or is the only thing that I can do is the following?
var myObject = {}
myObject[CONSTANTS.THING_TYPE] = 'whateverType';
myObject[CONSTANTS.THING_INFORMATION] = {};
myObject[CONSTANTS.THING_INFORMATION][CONSTANTS.THING_DESCRIPTION] = 'whateverDescription';
myObject[CONSTANTS.THING_INFORMATION][CONSTANTS.THING_NAME] = 'whateverName';

No you cannot do that using object literal initialization syntax.
So the only way is to use what you do in the second case - using [...] notatin.

Related

reading a local variable as an Object property [duplicate]

This question already has answers here:
Create an object with dynamic property names [duplicate]
(2 answers)
Closed 2 years ago.
I am trying to define a new Object type named attributes from an external config file, however I am not sure how to read the property dynamically as even without apostrophes it is being read as a string.
var dynAtt1 = vl.popupAttributes.att1 ;
var attributes = new Object({
dynAtt1 : { title: dynAtt1 }
})
dynAtt1 is being read literally instead of what it is defined as in the config.
You can use a computed property name.
var attributes = {
[dynAtt1] : { title: dynAtt1 }
}

Use string as object name in Javascript [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
Is it possible to access the property of a javascript object using a string?
example: I have a javascript object "obj" that contains a property 'index'.
obj.index = 4;
Now let's say I have a string whose value is the same as a property name of my object:
var str = "index";
Can I use the value of the str variable to access the 'index' property of 'obj'?
Just use the following code:
obj[str]
You can use array Notation (squire bracket notation )
obj[str]

How to refer a property in a value of another property in the same Javascript Object? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 7 years ago.
In a Gulp.js file (but it can be any Javascript file) I set the paths in a Javascript object:
var paths = {
devDir : 'builds/development/',
devDirGlobs : this.devDir+'*.html'
}
Now, as you can see, I'm trying to recall the property "devDir" in the value of the property "devDirGlobs", as "this.devDir".
It doesn't work, but also it doesn't give to me any error?
Any hint?
Thanks in advance for your help!
It's not possible to access the properties of the object before the object has been declared.
However, you could construct the object with dot syntax rather than as a literal.
var paths = {}
paths.devDir = 'builds/development/';
paths.devDirGlobs = paths.devDir + '*.html';
Or move common values into a shared config object.
var config = {
dir: 'builds/development'
};
var paths = {
devDir : config.dir,
devDirGlobs : config.dir + '*.html'
};

Using the value of a string variable as object parameter accessor [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I am attempting to dynamically access an object with a property value that is contained within a string. Example below:
var toolState = {
draw_point: false;
draw_line: false;
}
var dynamicText = "draw_point";
toolState.dynamicText = true; //here is the problem
I'm fairly new to JS. Sorry if this is a silly question.
Thanks
Use bracket notation instead of dot notation for variable names as properties.
toolState[dynamicText] = true;

Define Javascript object using dynamic string literal as object propery name [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 3 years ago.
Needing to do:
var some = {
`${foo1}_${foo2}`: bar
}
but this gives a syntax error though I must do it somehow. How?
you can suppose object as hashmap and access properties via []
var foo1 = 'a';
var foo2 = 'b';
var some = {};
some[foo1+'_'+foo2] = 'test';
console.log(some.a_b);

Categories