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
user2314737
29.7k20 gold badges108 silver badges126 bronze badges
asked May 4, 2015 at 13:06
2 Answers 2
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
answered May 4, 2015 at 13:16
Comments
var i =0;
fruits.forEach(function(entry){
i=i+1;
});
console.log(i);
answered May 4, 2015 at 13:22
Comments
lang-js
console.log(fruits.length)