简体   繁体   中英

Infinite loop to output all values from an array

From the function django I return JSON to JS

 paymentparking = paidparking.objects.filter(expirationdate__range=(startdate, enddate)).values('expirationdate','price')
    return JsonResponse({'result': list(paymentparking)})

Here I need to get all the expirationdate values. How can this be done and how can the cycle be organized?

 $.ajax({
    type: "POST",
    url: "statistics",
    data: {
      'startdate': finalDateStrStart,'enddate': finalDateStrEnd,
    },
    dataType: "json",
    cache: false,
    success:function (data) {
        for (let i = 0; i < 100; i++)
        {
                 console.log(data.result[i].expirationdate)
        }
        }
    });

You could maybe do something like this:

success: function (data) {
    if(data.result) {
        for (let i = 0; i < data.result.length; i++) {
            console.log(data.result[i].expirationdate)
        }
    }
}
        

Use Array.map() to iterate over elements and return an array of expirationdate.

success: function (data) {
  data?.result && data.result.map(obj => console.log(obj.expirationdate));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM