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

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

Sign up to request clarification or add additional context in comments.

Comments

3
var i =0;    

fruits.forEach(function(entry){

i=i+1; 
});

console.log(i);

Comments

Your Answer

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.