Wrogrsin
Wrogrsin

Reputation: 17

Uncaught TypeError: Cannot read property '0' of undefined - Wrogrsin

I defined my variable as:

var Players = [{
    name: "personA",
    score: 50
  },
  {
    name: "personB",
    score: 50
  },
  {
    name: "personC",
    score: 50
  }
];

and the error is:

for (v=0;v<T;v++)
{
    show += "<br/>" + Players.score[v];
}

i have already defined T and show

Upvotes: 0

Views: 622

Answers (2)

Konstantin Kreft
Konstantin Kreft

Reputation: 623

Players.score[v]is undefined.

It should be Players[v].score.

You defined your Players variable as an array, so you have to loop over this array and not the score key.

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

You forgot to specify which Player. Change the code to use Players[v].score:

for (v = 0; v < T; v++) {
    show += "<br/>" + Players[v].score;
}

The Players is an array, not the score.

Upvotes: 1

Related Questions