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 };
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 want to achieve this:
I've tried this:
userId: {
isAdmin: false,
}
userId is a const where its value is like 447785 but the resulted object just show userId as a property not a value.
var users = {}
users[userId] = {isAdmin: false}
Feels like this is probably a dupe of a Q on SO that explains it properly. I'll have a look...
Use this
{
[userId]: {
isAdmin: false
}
}
More here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015 and here http://www.benmvp.com/learning-es6-enhanced-object-literals/#computed-property-keys
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);
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
JavaScript set object key by variable
(8 answers)
Closed 5 years ago.
How can I use a variable as the name parameter in an object?
var field = 'profile.email'
var value = 'example#email.com'
var query = { field : value };
console.log(query);
Current output:
{ field : 'example#email.com' }
Output I want to achieve:
{ 'profile.email' : 'example#email.com' }
You can e.g. drop the field variable inside square brackets.
var field = 'profile.email',
value = 'example#email.com',
query = { [field] : value };
console.log(query);
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);