Jason Khalili Jason Khalili - 3 months ago 13
Javascript Question

JSON select from array

I have the following JSON object:

[{"_id":"57b1f03ec4f37923cc436100","pokedex_id":134,"location":{"type":"Point","coordinates":[-117.4955084,34.0175467]}},
{"_id":"57b1f03fc4f37923cc436101","pokedex_id":134,"location":{"type":"Point","coordinates":[-117.4955084,34.0175467]}}]


I'm trying to access the data with
console.log(temp[0]);
, but for some reason my output is just
[
. What am I doing wrong? Also, any subsequent index just seems to return the corresponding char value. For example:
temp[1]
returns
{
,
temp[3]
returns
_
.

Answer

In JavaScript, there is no such thing as a JSON object.

You have a JSON text, which is a string.

somestring[0] gives you the first character of that string.

Use JSON.parse() to convert your JSON text into a JavaScript array.

Comments