const people = {
person1: {
first: "Yassine",
last: "Boutabia",
},
person2: {
first: "Md Jahidul Hasan",
last: "Mozumder",
},
person3: {
first: "Md Feroj",
last: "Ahmod",
},
}
retrieveInfo = () => {
var propName = "first"; //an idea that occurred to me
var array = [];
for (var key in people) {
if (people.hasOwnProperty(key)) {
array.push(people[key]);
}
}
console.log(array[0][propName]);
}
the variable array
represents the people
objects as an array here. When I write console.log(array[0].first);
, it outputs the first
of person1
which is Yassine
. So far so good.
Now, I am trying to retrieve this value using a variable. I want to store either the first
or last
in a variable and append it to the end of array[0]
so I can get that specific value. How can this be achieved?