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}`]()
Related
This question already has answers here:
In JavaScript, how to conditionally add a member to an object?
(29 answers)
Closed 2 years ago.
I'm aware with ES6 JavaScript object you can dynamically declare variables as object keys, using [], for example:
{[2 + 2]: "four!"}
gives the output {"4": "four!"}
the question is, can a similar method be done in order to add an entire property, through variables etc., inline? Meaning, like suppose I have the following test object:
var myObj = {
someProp: 2,
someOtherProp: 3 //only add this prop if a condition is met
}
is there anything I can write in the inline object for someOtherProp, to only have it be added to the object, if a certain condition is met? So for example (pseudocode), something like this
var myObj = {
someProp: 2,
[someBool ? null : "someOtherProp: 3"] //only add this prop if a condition is met
}
would give the ouput (considering someBool is true), like the above, but if someBool is false, it would give me
var myObj = {
someProp: 2
}
??
I'm aware I can simply add a property (or delete one) to (/from) the object later, using the [] indexer, like
someBool && (myObj["someOtherProp"] = 3)
as well as make some kind of helper function for this,
but I was wondering if there was a way to do it using the inline-object notation?
You could spread an object with a conditional operator.
{} is a neutral value.
var myObj = {
someProp: 2,
...(someBool ? {} : { someOtherProp: 3 })
}
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
Is there a way to pass a parameter into a function and use it to dynamically access json data? (Assume all my syntax is correct. I'm just wondering if this is possible)
I'm using React to try and pass a parameter into a functional component and get data from the json file to type out {Component(thing1)} instead of {Component({jsondata.thing1.greeting}, {jsondata.thing1.closing})} for each key pair I need to use. I can already access all the data, I would just rather have all the data calls in the component file and pass in a single parameter that corresponds to the object I need, rather than have to pass in each key as a separate parameter.
This is kind of what I'm thinking:
//json data
{
jsondata: {
thing1: {
greeting: "Hey"
closing: "Bye"
}
thing2: {
greeting: "Sup"
closing: "See ya"
}
thing3: {
greeting: "Yo"
closing: "Farewell"
}
}
}
// js file
function = (INSERT) => {
console.log ({data.INSERT.greeting}, {data.INSERT.greeting})
}
function(thing1) //should return "Hey, Bye"
function(thing2) //should return "Sup, See ya"
If you want to specify dynamic key use could use square brackets syntax.
for example: json_data[dynamic_key]
function = (INSERT) => {
console.log ({data[INSERT].greeting}, {data[INSERT]greeting})
}
What you're looking for is Bracket Notation [] Property Accessors
These allow you to dynamically get data from Objects like so:
key = 'a string'
object = {
'a string': 412123,
'another one': 440
}
console.log(object[key])
console.log(object['another one'])
What you're using is the Dot Notation . property accessor. Try this one instead.
const yourFunction = (INSERT) => console.log({data[INSERT].greeting}, {data[INSERT].greeting})
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:
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:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
Imagine I have an object like:
var obj = {
name: {
value: 'Sergio'
},
lastName: {
value: 'Tapia'
}
}
I want to create a function that grabs the value of a given property.
Ideally:
console.log(getProperty(obj, 'name'));
=> 'Sergio'
console.log(getProperty(obj, 'lastName'));
=> 'Sergio'
You can use bracket notation to access the property on the object. Your function would be:
function getProperty(obj, property) {
return obj[property].value;
}
I would probably name it getProperyValue instead.
function getProperty(obj,property){
return obj[property].value;
}
This function should help you achieve what you need.
function getProperty(obj, key){
return obj[key].value;
}
I believe
console.log(getProperty(obj, 'lastName'));
should return 'Tapia' rather than 'Sergio'.