如何在node.js中查找数组的长度

时间:2022-09-28 18:29:04

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

如何在node.js中将数组计数为2

2 个解决方案

#1


var i =0;    

fruits.forEach(function(entry){

i=i+1; 
});

console.log(i);

#2


While

fruits.length

will give you the highest index plus 1 (86)

会给你最高指数加1(86)

you can use

您可以使用

fruits.filter(function(x){return x !== 'undefined'}).length

to get the number of non-undefined elements in the arrya

获取arrya中非未定义元素的数量

#1


var i =0;    

fruits.forEach(function(entry){

i=i+1; 
});

console.log(i);

#2


While

fruits.length

will give you the highest index plus 1 (86)

会给你最高指数加1(86)

you can use

您可以使用

fruits.filter(function(x){return x !== 'undefined'}).length

to get the number of non-undefined elements in the arrya

获取arrya中非未定义元素的数量