Here is the array I am working with:
[
{
"id": 2,
"language": {
"name": "English",
"abbreviation": "EN"
}
]
To access the value of language.name
:
function get(arrName)
{
for(var k = 0; k < arr.length; k++)
{
console.log(arr[k].language.name); // English
}
}
In the expression arr[k].language.name
, I want to make 'language' dynamic by getting it from the parameter of the function, arrName
. So it should look like this:
function get(arrName)
{
for(var k = 0 ; k< arr.length; k++)
{
var dynamicArr = '.' + arrName + '.name';
console.log(arr[k] + dynamicArr);
}
}
However, the output currently shows:
[object Object].language.name;
How can I retrieve the actual value from the array?