I am currently trying to extract the names, age and nation of each player inside the object. I used object keys and it was able to get the position name.
Note: This is vanilla Javascript
const arsenal = {
name: "Arsenal",
city: "London",
startingLineup: {
gk: { name: "Aaron Ramsdale", age: 24, country: "England" },
lb: { name: "Oleksandr Zinchenko", age: 26, country: "Ukraine" },
rb: { name: "Ben White", age: 25, country: "England" },
lcb: { name: "Gabriel Magalhaes", age: 25, country: "Brazil" },
rcb: { name: "William Saliba", age: 22, country: "France" },
cdm: { name: "Thomas Partey", age: 29, country: "Ghana" },
cm: { name: "Granit Xhaka", age: 30, country: "Switzerland" },
cam: { name: "Martin Odegaard", age: 23, country: "Norway" },
lw: { name: "Gabriel Martinelli", age: 21, country: "Brazil" },
rw: { name: "Bukayo Saka", age: 21, country: "England" },
st: { name: "Gabriel Jesus", age: 26, country: "Brazil" },
},
};
Object.keys(arsenal.startingLineup).forEach((pos) => {
const name = arsenal.startingLineup[pos.name];
console.log(pos, name);
});
But when I try to get the other information it comes out as undefined
. How can I get the rest of the player info?
gk undefined
lb undefined
rb undefined
lcb undefined
rcb undefined
cdm undefined
cm undefined
cam undefined
lw undefined
rw undefined
st undefined