reading a local variable as an Object property [duplicate] - javascript

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

Related

Json Data Acess [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I have below json data in the variable .
var links = {
info: {
title: "Informatics "
},
busi: {
title: "Business"
},
lang: {
title: "Language"
}
};
In the code i have the variable named type which could have string as info,busi,lang and that variable is getting as a function argument like
function get_data(type)
{
var data = JSON.parse(links);
// Now i want to access the title of that particular type only I tried to use this but it didnt work
// data.type
// where as if i use the exact word it shows me data like this data.info
}
I want to make the code more generalize rather than sticking to constants like info, busi ,land . Any suggestions how can i make it more generalize ?
To reference dynamic property names, rather than statically, you need square bracket, not dot, syntax.
data.type - looks for a property named 'type'
data[type] - looks for a property whose name is contained within a variable type

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;

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

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.

Adding custom variables to an object [duplicate]

This question already has answers here:
How to convert string as object's field name in javascript
(5 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
Suppose I have an object obj which has some variables in it. What I want to do is to add a new variable which has a name i gave as a parameter.
For example:
var obj=
{
number:8
}
function AddField(field_name,obj)
{
//some magical code here
}
If I call
AddField('name',obj);
i want to be able to do
obj.name='Apple'
after calling this function.
Any ideas?
Use square brackets notation:
obj[field_name] = value;
REF: http://www.jibbering.com/faq/faq_notes/square_brackets.html#vId
Use the bracket notation:
obj[field_name] = void 0;
This creates a new property with an undefined value.
If obj is already defined, you can just do obj.field_name = null or obj["field_name"] = null.
I wouldn't set any value to undefined even though the language allows for that, simply for a semantic issue: undefined means "this property/variable has not been defined" and null means "this property/variable has no value".

Categories