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);
Related
This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]
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;
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.
This question already has answers here:
Is there a way to access a javascript variable using a string that contains the name of the variable?
(4 answers)
How to use a string as a variable name in Javascript? [duplicate]
(3 answers)
parse a string as an object from data attribute [duplicate]
(1 answer)
Use dynamic variable names in JavaScript
(19 answers)
Closed 9 years ago.
How can I make the code that follows to work?
var x = 'name';
and then, to use the value inside x, as it was a variable, and set it, so that if i want it to be NAME, i'll have the result:
var name = 'NAME';
Is it possible ?
Not directly, no. But, you can assign to window, which assigns it as a globally accessible variable :
var name = 'abc';
window[name] = 'something';
alert(abc);
However, a much better solution is to use your own object to handle this:
var name = 'abc';
var my_object = {};
my_object[name] = 'something';
alert(my_object[name]);
I haven't seen the rest of your code, but a better way might be to use an object.
var data = {foo: "bar"};
var x = "foo";
data[x]; //=> bar
var y = "hello";
data[y] = "panda";
data["hello"]; //=> panda
I think this is a little cleaner and self-contained than the window approach
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
var tinymce_toolbar = {}
tinymce_toolbar.__default =
{
script_url: '/cms/libs/js/manual/renders/tiny_mce/tiny_mce.js',
};
tinymce_toolbar.__simple =
{
script_url: '/cms/libs/js/manual/renders/tiny_mce/tiny_mce_simple.js',
};
// Doesn't work
var t = $(this).find('input[name=toolbar]').first().val();
$('.RenderHtmlEditor').tinymce(tinymce_toolbar.t);
// works
var t = $(document).find('input[name=toolbar]').first().val();
$('.RenderHtmlEditor').tinymce(tinymce_toolbar.__default);
$('.RenderHtmlEditor').tinymce(tinymce_toolbar.__simple);
how i do it to be dynamic? Thanks
object['name'] is quite same way as object.name. simply assign a associative attribute and use it as a property.
Instead of dot notation,
tinymce_toolbar.t
Use subscript notation:
tinymce_toolbar[t]