0

Preface - I'm a coding newbie, and I really tried to find an answer to this question.

I'm using jQuery to make an AJAX request, and my response is a stack of arrays. While assigning the data I need to variables in a for loop, all of them work fine except for this one:

var x = response[i].pets.media.photos.8.val;

I'm getting an "unrecognized number" error about the "8" in the array path, but that is the name of the directory the photo is in. Am I missing some formatting? What am I doing wrong?

Thanks in advance for the help.

  • Properties with numerical names cannot be referenced using dot notation. Use bracket notation (e.g. `photos["8"].val`) instead. – Heretic Monkey Jan 22 '18 at 20:03

1 Answers1

0

You will have to use square bracket notation to access properties that would be syntactically invalid in dot notation

var x = response[i].pets.media.photos.['8'].val;
Musa
  • 96,336
  • 17
  • 118
  • 137