Getting Javascript object property by name [duplicate] - javascript

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'.

Related

Calling a function in JS using string [duplicate]

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}`]()

Retrieving the variable value to act as a name [duplicate]

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 };

How to access sub-values of a multi-dimensional object [duplicate]

This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 7 years ago.
is it possible to get a reference to an object with the object itself
obj
and the attributes in string form
'address.town.street'
so that at the end it resolves
obj.address.town.street
i could immageine smth like the eval() function.
Try
function getValue(obj, path) {
return path.split(".").reduce(function(obj, name){ return obj[name]}, obj);
}
Do not use eval. Use this instead
Object.prototype.nestedByString=function(reference){
var current=this;
path=reference.split(".");
for(var i=0;i<path.length;i++){
current=current[path[i]];
}
return current;
}
Here is a demo
I suppose that if you're allergic to extending native prototypes, you can do this
function nestedByString(obj,reference){
var current=obj;
path=reference.split(".");
for(var i=0;i<path.length;i++){
current=current[path[i]];
}
return current;
}

jQuery expression as object key [duplicate]

This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I need to figure out how to create a dynamic key string for an object. This expression makes JavaScript complain.
return {$(this).val(): true}; // returns an object e.g. {2: true}
What am I doing wrong?
You have to create the object, then use bracket notation for the dynamic key
var obj = {};
var val = $(this).val();
obj[val] = true;
return obj;
or a completely unnecessary one-liner
return (function(o,e) {o[e.value]=true; return o;})({}, this);
The JavaScript object literal syntax {x: y} specifies that x will be a (possibly) quoteless string, and y any value. You can't use this syntax for dynamic keys.
Use this instead:
var foo = {};
foo[$(this).val()] = true;
return foo;

Javascript variable object keys [duplicate]

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);

Categories