Each in JSON jquery date

Asked

Viewed 7,571 times

1

I’m trying to walk a array de objetos with jquery I’m doing so:

    $.getJSON('http://localhost:8000/produtos', function(data) {
        $(data).each(function (item) {
            console.log(item);
            $('.product-slider').append(item.NmProduto);
        });

        console.log(data);
    });

In the first console.log(item) is coming out 0 1 2 3. Already in charge $('.product-slider').append(item.NmProduto); is doing nothing and if I log console on item.NmProduto sai undefined, already in my console.log(data) is coming the right information as in the following print:

console.log da data vindo do jquery

I need to set an html in this div with the data coming from json, what is the best way to develop this ?

1 answer

3


You’re getting the chave of array and needs the truth of value, then use $.each thus:

$.getJSON('http://localhost:8000/produtos', function(data) {
    $.each(data, function (key, item) {
        //console.log(item);
        $('.product-slider').append(item.NmProduto);
    });
});

Examples of using jQuery array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.