JQuery "TypeError: invalid" in 'operand a "

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

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

when trying to print 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 the following:

data1

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

and therefore I cannot figure out how to use this array! Does anyone have an idea?

+4
source share
4 answers

You repeat the line, you do not need two functions .each ()

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

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

+2
source

loopin $.each():

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

data.fields - , strings, . , {key:value}.

0

In your main "field" loop, var is not an array, so you cannot use "each" on it.

-2
source

Source: https://habr.com/ru/post/1546130/


All Articles