Question:
What is producing the undefined error in my loop?
Product:
Using JavaScript on my webpage to perform a data query.
What are you trying to achieve:
I am trying to create a loop (attempting foreach) to loop through my array of objects and do a .find on one of to datasets depending on the Div that each player is in and return a value called “RunningPlace”.
The values for Div = “FPO” are in dataset “liveResultsFPO”
The values for Div = “MPO” are in dataset “liveResultsMPO”
What have you already tried:
Tried google, youtube, forum searches and can’t quite figure it out. The results are very strange to me (new to javascript).
Here is my array I am trying to loop through:
pNames = [
{
Name: "Holyn Handley",
Div: "FPO"
},
{
Name: "Simon Lizotte",
Div: "MPO"
},
{
Name: "Paul McBeth",
Div: "MPO"
},
{
Name: "Richard Wysocki",
Div: "MPO"
}
]
Additional information:
Here is my code:
pNames.forEach((obj) => {
console.log(obj.Name + "-" + obj.Div)
if(obj.Div === "FPO"){
dataSet = liveResultsFPO;
}else if(obj.Div === "MPO"){
dataSet = liveResultsMPO;
}
liveResult = dataSet.data.scores.find(a => a.Name === obj.Name);
console.log(liveResult)
})
My results are great for Holyn Handley and Paul McBeth.
The results are undefined for Simon Lizotte and Richard Wysocki.