Fetch value by dynamic key from javascript object [duplicate] - javascript

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

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

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

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.

Dynamic Variable in JS [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I have this code :
var dynamicVarName = id;
var percentage = res.parkeren[0].dynamicVarName.percentage;
DynamicvarName can have different values, but when i call this in the var percentage it's not working.
How can i get the dynamic variable in the array / object?
You need the bracket notation for the access:
var dynamicVarName = 'id';
var percentage = res.parkeren[0][dynamicVarName].percentage

jQuery use variable as array key [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 8 years ago.
var diena = "example"
array.push({diena: sub_array});
I want to array key use variable and have key "example" not "diena". How I can do this ?
You'll need to construct the object beforehand.
var diena = "example";
var obj = {};
obj[diena] = sub_array;
array.push(obj);

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?

Categories

Resources