Linto P D
Linto P D

Reputation: 9165

How to find the length of an array in node.js

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

Upvotes: 0

Views: 2978

Answers (2)

Viva
Viva

Reputation: 2075

var i =0;    

fruits.forEach(function(entry){

i=i+1; 
});

console.log(i);

Upvotes: 3

user2314737
user2314737

Reputation: 29317

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

Upvotes: 3

Related Questions