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
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:
When to use ":"(colon) operator in javascript vs "=" operator?
(3 answers)
Closed 3 years ago.
I have a onSubmit() method in Vuejs. Couldn't figure what is the difference between equal = and colon : sign. Here is the following cod
methods:{
onSubmit(){
let product= {
name: this.name,
review: this.review,
rating: this.rating
},
this.name=null,
this.review=null,
this.rating=null
}
}
In the above function I am storing values in object called product, after storing values I am assigning null values to data variables.
Why we are passing values to product object using : and to data object using = sign?
{
name: this.name,
review: this.review,
rating: this.rating
}
This creates an object, which is a data structure.
let product = {}
Here you are assigning the object (data structure) to a variable.
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 use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
I would like to know if there is a clean way to set the value of a key from a string variable when using spread syntax in es6?
Something like the following:
let keyVar = 'newKey'
let newObject = {keyVar:{some:'json'},...oldObject}
But this leads to:
{"keyVar":{"some":"json"}, ... }
rather than:
{"newKey":{"some":"json"}, ... }
You can use computed properties:
const keyVar = 'newKey';
const newObject = { [keyVar]: { some: 'json' } };
console.log(newObject);
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);