This question already has answers here:
How to set object property (of object property of..) given its string name in JavaScript?
(16 answers)
Dynamic deep setting for a JavaScript object [duplicate]
(3 answers)
Closed 8 years ago.
Consider the following code:
var obj = {};
obj.bar = 'w00p!'; //works
obj.foo.bar = 'there it is'; //throws error because bar is not set
Could you ever make obj.foo.bar = work if obj.foo is not set?
Is there an elegant way to set a deep variable in an object in JavaScript? In the same spirit as mkdir's -p option: mkdir -p a/deep/dir
Related
This question already has answers here:
Why can I change a constant object in javascript
(12 answers)
Const in JavaScript: when to use it and is it necessary?
(18 answers)
Closed 8 months ago.
"Const variables allow an object sub-properties to be changed but not the object structure."
What means "object structure"?
Because this code is compiling:
const numbers = [1, 2, 3]; // inferred to type number[]
numbers.push(4); // no error
May anyone give a compiling error of a sentence after declaring the const "numbers" in my example?
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:
Self-references in object literals / initializers
(30 answers)
Access object properties within object [duplicate]
(2 answers)
Closed 6 years ago.
How to do ElementName get value from ElementId?
var Elements = {
ElementId: '#myelement',
ElementName: Elements.ElementId
};
alert(Elements.ElementName);
You haven't created the object yet, so it's not going to be defined. You can define that property on the object once it's created like this:
var Elements = {
ElementId: '#myelement'
};
Elements['ElementName'] = Elements.ElementId;
alert(Elements.ElementName);
However I think there's a better way; what specifically are you using the name and id properties for?
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
What does window['someKeyword'] mean in JavaScript?
I guess the window is the DOM window object, but the syntax to retrieve a property is window.propertyName in the reference I found, not window['propertyName']. Are these two ways to retrieve the property?
To access a field of an object in Javascript there are two ways.
let's this is an object
obj = {name:'Object1'};
You can access name field by two ways
obj.name // returns 'Object1'
obj['name'] //returns 'Object1'
besically [] this method is useful when you have to access a field using a variable like bellow
var obj = {name:'Object1'}
var field = 'name'
obj[field] //returns 'Object1' because it will put value of field in `[]`
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);