i have an array
var fruits = [];
fruits[5] ="apple";
fruits[85] ="pinapple";
How can i get the count of array as 2 in node.js
var i =0;
fruits.forEach(function(entry){
i=i+1;
});
console.log(i);
While
fruits.length
will give you the highest index plus 1 (86)
you can use
fruits.filter(function(x){return x !== 'undefined'}).length
to get the number of non-undefined elements in the arrya
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.