0

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
6
  • what exactly are you asking? Commented May 4, 2015 at 13:08
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… console.log(fruits.length) Commented May 4, 2015 at 13:08
  • @DasDas.I need to get the length of given array Commented May 4, 2015 at 13:09
  • var l = fruits.length; Commented May 4, 2015 at 13:10
  • 1
    @LintoPD , sorry . You wanted the number of defined items from array , not the actual length . :) Commented May 4, 2015 at 13:32

2 Answers 2

3

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

3
var i =0; 
fruits.forEach(function(entry){
i=i+1; 
});
console.log(i);
answered May 4, 2015 at 13:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.