This question already has answers here:
JavaScript - Why can't I call a variable "name"?
(2 answers)
Closed 5 years ago.
My question is pretty simple. I'm creating two objects. The second object is referencing an object inside the first object.
var me = {
name: {
first: "justin"
}
};
var name = me.name;
console.log(me.name.first); // "justin"
console.log(name.first); // undefined
Why am I getting undefined in my second console log? Shouldn't I get "justin" instead?
You need to use another name. There is a name variable which is global.
var me = {
name: {
first: "justin"
}
};
var anotherName = me.name;
console.log(me.name.first);
console.log(anotherName.first);
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
I have an array of objects for example this object:
new: {
name: 'New',
image: <LightningFilter style={styles.queryImage}/>,
selectedImage: <LightningWhiteFilter style={styles.queryImage}/>,
dataFunc_Chefs: getNew_Chefs,
dataFunc_Recipes: getNew_Recipes,
},
and I have a variable to check if I am selecting Chefs or Recipes
what I want to do is for example if Chefs is selected call dataFunc_Chefs how can I do that using string ?
For example, I want to call this:
var category = 'Chefs';
`new.dataFunc_${category}()`
I know this doesn't work but this is what I mean
I know I can use if and else and check the variable and call the function accordingly, but lets say I have many functions how do I acheive that
Hope this is what you are looking for.
You just need to access dataFunc_Chefs object from your object using string literals.
function getNew_Chefs() {
console.log('getNew_Chefs executed');
}
function getNew_Recipes() {
console.log('getNew_Recipes executed');
}
const myObj = {
name: 'New',
dataFunc_Chefs: getNew_Chefs,
dataFunc_Recipes: getNew_Recipes,
}
var category = 'Chefs';
myObj[`dataFunc_${category}`]()
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I need an object in Typescript declared like this:
queryParameters = { flagged: true };
Now I would like to have the flagged to be retrieved from a variable name. Something like:
var param = 'flagged';
queryParameters = { ValueOf(param): true };
Any idea ?
Thanks.
Why not use computed property names:
queryParameters = { [param]: true };
This question already has answers here:
How to find JavaScript variable by its name
(7 answers)
Closed 7 years ago.
I saw a lot of answer to call a method from a given object using a string, but no one to get the object itself.
I would like something like that
var a
var b
var c
function getObject(objectAsString)
{
return getMyObject(objectAsString);
}
then if I write
var obj=getObject("a")
my result is obj=a
Is there a function "getMyObject"?
Thanks
See following code
<script>
//in one script
var GlobalvarName = 500;
alert(window["GlobalvarName"]); //alert is : 500
</script>
you could do:
var hello = 'Hello World';
this['hello'];
I wouldn't make those variables so global though. here's why
Instead put them inside an object like:
var obj = {
a: 'Hello',
b: 'World'
}
console.log(obj['a'], obj['b']);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have this JSON, I am trying to retrieve values by substituting variables as below.
var model = {
regression : {
regUtils : {
subIDs : {
B00099999823 : {
"a" : "b",
"c" : "d"
}
}
}
};
var vals = "B00099999823";
alert(model.regression.regUtils.subIDs.vals.a); // error returned : Object doesn't support this property or method
alert(model.regression.regUtils.subIDs.B00099999823.a); //alerts b
How can I substitute a variable in place of a key or object.
thanks.
You can access with a variable contents like this:
var vals = "B00099999823";
alert(model.regression.regUtils.subIDs[vals].a;
You can use [] to access an object by using a variables contents. It would be the same as this:
model.regression.regUtils.subIDs['B00099999823'].a
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 8 years ago.
I am attempting to add a variable key, with no luck.
Here's what I got so far:
mysql('translations',{
check: 'element_id',
element_id: element_id,
{'lang-'+lang_id}: value
});
The variable key is the last line of the function.
Any ideas?
You can't use expressions for the identifiers in an object literal.
First create the object, then you can use dynamic names:
var obj = {
check: 'element_id',
element_id: element_id,
}
obj['lang-'+lang_id] = value;
mysql('translations', obj);
You can do this:
var x = {
check: 'element_id',
element_id: element_id
};
x['lang-'+lang_id] = value;
mysql('translations', x);