How to access to object properties by string with another object [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I need to access to object properties by string with another object
In php you can reach that like if array $b = 't';$a->$b; is the same as $a->t
var obj = {
title : 'spider'
}
var hero = {
spider : 'is a hero'
}
console.log( hero.spider );//works fine
console.log( hero.obj.title );//i need the same result, from variable (obj)
how can i do that

console.log(hero[obj.title])
I think this should work.

Related

How to replace .Id with a variable string value [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
I currently have the following in my Javascript code where Id is a field of an object:
this.recId = event.detail.row.Id;
I want to make the Id part generic something like
String a = 'Id';
this.recId = event.detail.row.a;
How do I achieve this in Javascipt
Use [ ] instead.
const obj = {
id: "myObject"
}
const a = "id"
console.log(obj[a]);
In your example, it would be
this.recId = event.detail.row[a];
You can use
this.recId = event.detail.row[a] to get the desired result

Fetch value by dynamic key from javascript object [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I just want to fetch object values by dynamic key, I need this because of web service providing data in this form
var apiData = [{first_name:"ashwani"},{second_name:"raju"},{third_name:"ravi"}]
var naming=["first","second","third"]
for(i=0;i<apiData.length;i++){
var symKey=naming[i] + "_name";
console.log(apiData[i].symKey)
}
But I am getting undefined, how can I do that ?
Use brackets [] instead of . notation.
.symkey will look for a property called symkey in the object.
var apiData = [{first_name:"ashwani"},{second_name:"raju"},{third_name:"ravi"}]
var naming=["first","second","third"]
for(i=0;i<apiData.length;i++){
var symKey=naming[i] + "_name";
console.log(apiData[i][symKey])
}

Use a variable to a variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I want to pass a variable into a variable
This is what I want
uk.objects.ARG
I have
var d= ARG
how can I get uk.objects.ARG?
I tried
uk.objects.d
, javascript stores it as the string, not a variable
What should I do in order to make it as a variable ?
I think what you are looking for is perhaps:
var d = "ARG";
uk.objects[d];
You can access JavaScript objects' properties also with the bracket notation:
object[property] = value;
The same works for reading. So in your case you could do the following:
var value = uk.objects[d]

get value with string key in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 9 years ago.
I can't figure out how to get an object property using a string representation of that property's name in javascript. For example, in the following script:
consts = {'key' : 'value'}
var stringKey = 'key';
alert(consts.???);
How would I use stringKey to get the value value to show in the alert?
Use the square bracket notation []
var something = consts[stringKey];
Javascript objects are like simple HashMaps:
var consts = {};
consts['key'] = "value";
if('key' in consts) { // true
alert(consts['key']); // >> value
}
See: How is a JavaScript hash map implemented?

Accessing Object and Property via Variable? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I have a namespace setup like
mySpace.util = {
valid: {
first: function() {
//blah
}
}
}
I am trying to pass the valid and first as variables to access the function ? I've tried:
mySpace.util.[variable1[variable2]()] but that doesn't work ? I am trying to pass variables so I get mySpace.util.valid.first()
Any ideas how I could do this?
var obj = 'valid',
meth = 'first';
mySpace.util[obj][meth]();

Categories

Resources