0

I'm trying to access an array inside of an object that comes in a response:

getMatches().then((matches) =>{
let possibleDraws = []
console.log(matches)
console.log(typeof matches)
matches.forEach(match => {
    if(isPossibleDraw(match)){
        possibleDraws.push(match)
    }
    console.log(match.homeForm)    
}) 
console.log(possibleDraws)}).catch((err) =>{
console.log(err)})

console.log(matches) returns correctly an array with just one value for testing purposes:

0:
awayForm: (4) ['L', 'D', 'W', 'D']
awayTeamID: 15994
awayTeamName: "Maritzburg United U23"
homeForm: (4) ['L', 'D', 'W', 'D']
homeTeamID: 15990
homeTeamName: "Chippa United U23"
leagueID: 734
matchID: 793855
season: 2021
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)

However, when accessing match.homeForm, it returns "Undefined is not an object", even when match.homeForm does exist (based on the last console.log) . I even mocked the response of getMatches() to the same array (original function calls an API and get the response as a promise) and it works perfectly fine, so I really have no idea what could be happening here.

  • That 0: is a key to an array so there is no `match` object as far as the OP code shows. Try `match[0]` if `match` is the variable name of that array. – zer00ne Feb 12 '22 at 20:19
  • It doesnt work either. What is pretty strange is that i can actually get values like match.matchID or match.awayTeamID, the error only happens for the object attributes that are arrays – Paolo Flores Feb 12 '22 at 21:41

1 Answers1

0

if you try match[homeForm] or match['homeForm'] does it work?

also if it is an array then you need to select first item matches[0]

andys
  • 94
  • 6