Prasad Prasad - 5 months ago 7
jQuery Question

$.each throwing error : Cannot use 'in' operator to search for '37' in

I have a JSON Object like below :

var data = "{'key1' : 'value1', 'key2' : 'value2'}";


As it is in JSON format, no parsing required, so I just want to iterate the object using $.each as below:

$.each(data, function(index, value) {
alert(index + ": " + value);
});




It is throwing a console error

Uncaught TypeError: Cannot use 'in' operator to search for '37' in {'key1' : 'value1', 'key2' : 'value2'}(…)


I don't have any idea of what's going wrong. What is it?

Answer

You just need to change the way value assigned to data variable to something like below:

var data = {key1 : 'value1', key2 : 'value2'};

in place of

var data = "{'key1' : 'value1', 'key2' : 'value2'}";