Uncaught TypeError: Cannot use 'in' operator to search for 'length' in

Iván Alberto Fontalvo Salgado picture Iván Alberto Fontalvo Salgado · May 16, 2015 · Viewed 254.9k times · Source

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in "

This is the error I receive when I try to do a $.each to this JSON object :

{"type":"Anuncio","textos":["Probando esto","$ 20150515"],"submit":"codParameters?___DDSESSIONID\u003d14EA4721A904D6DD71591156996E29F7%3A%2FMobilTest"}

I have also tried to do the same with stringify, but I receive the same error:

{\"type\":\"Anuncio\",\"textos\":[\"Probando esto\",\"$ 20150515\"],\"submit\":\"codParameters?___DDSESSIONID\\u003d06CBEC9D1A53616EFF703A8C71FBC2B4%3A%2FMobilTest\"}"

If I remove parameters ___DDSESSIONID\\u003d06CBEC9D1A53616EFF703A8C71FBC2B4%3A%2FMobilTest from the object the $.each works fine.

Why might this be happening?

Answer

Felix Kling picture Felix Kling · May 16, 2015

The in operator only works on objects. You are using it on a string. Make sure your value is an object before you using $.each. In this specific case, you have to parse the JSON:

$.each(JSON.parse(myData), ...);