How to Count data in for loop nodejs

var service_count = 0;
for (let i = 0; i < res.length; i++) {
    service_count += i;  
    console.log('service_charge_count: =====> '+service_count);

2 Answers

Radonirina Maminiaina On

Replace i with service_count, then you can change your code with:

for(let service_count  = 0; service_count  < res.length; service_count ++){
    console.log('service_charge_count: =====> ' + service_count);

Or I think that you service_count is res.length, therefore, you can do:

let service_count = res.length;
wasanga7 On

By adding i to your variable, you're adding in the first loop 0, in the second loop 1, in the third loop 2. So you're not counting anything.

var service_count=0;
for(let i=0;i<res.length;i++){
console.log('service_charge_count: =====> '+service_count);

By that you'll see how many iterations has had the loop. If the final value of service_count is the length of the variable res you can do this:

var service_count = res.length;
console.log('service_charge_count: =====> '+service_count);