Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery "TypeError: invalid 'in' operand a"

I have the following json array returning from an ajax call:

{"err":"err_type","fields":["field1","field2"]}

when tryin to print it out with this function:

$.each(data.fields, function (i, field) {
    console.log(field);
    $.each(field, function (j, f) {
        $('[name="'+f+'"]').addClass('form_err');
        console.log(f);
    });
});

i get this:

data1

TypeError: invalid 'in' operand a
...turn function(b){return db(a,b).length>0}}),contains:fb(function(a){return funct...

and so i can't figure out how to use this array! Anyone have any idea?

like image 930
Mariano Avatar asked Dec 19 '22 13:12

Mariano


2 Answers

You are iterating a string, you don't need two .each() functions

$.each(data.fields, function (i, field) {
    $('[name="'+field+'"]').addClass('form_err');
    console.log(field);
});
like image 196
Anton Avatar answered Dec 24 '22 00:12

Anton


Remember to add dataType: 'json'; so that it can be looped as an array and not a string.

like image 27
Waweru Kamau Avatar answered Dec 24 '22 02:12

Waweru Kamau