简体   繁体   中英

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

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.

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