Ivan Ivan - 6 months ago 26
JSON Question

How to fix fetching JSON data on inside of loop with limit number?

There is something bothers me.
I'm trying to understand this output in console, take a look

loop and json data

listdata is a global variable.

I can use listdata.orders[x].id_order to return any numbers without errors.

Then I use for() to make a list and it gave me an error that doesn't make sense =/

EDIT: It needs to be limited as 9 items, not complete as data.length

Answer

Use this

listdata.orders.forEach(function (order) {
    console.log(order.id_order);
});

To limit the iteration to 9 orders.

Method 1:

listdata.orders.slice(0, 9).forEach(function (order) {
    console.log(order.id_order);
});

Method 2:

listdata.orders.some(function (order, index) {
    if (index > 9) {
        return true;
    }
    console.log(order.id_order);
});