1

How do I get the number of elements in an array with non-consecutive numbers as keys?

var array = [];  
array[5] = "something";  
array[10] = "nothing":  

expected:
number of elements in array = 2

actual:
instead I get the last number used as the "length", 11

I can figure out the way to do this by iterating through each element. Is there is better way to do this?

4

4 Answers 4

2

You may count non empty cells:

array.filter(function(e){return e!==undefined}).length
Sign up to request clarification or add additional context in comments.

5 Comments

I think your method is the best, though array.filter(Boolean) would be more concise. Also, could you explain your answer a bit more?
@RobM. What is Boolean?
@4castle it's the object wrapper around Boolean values (link). In filter calls it can serve as an identity function though, testing for truthy values
@RobM. Ahh, nifty. It would work in this case perhaps, but it would also filter out 0 and "", which may be valid values.
@Nosyara correct. Definitely not applicable for all use cases, but based on the data OP posted I think it works here.
0

Sounds like what you actually want is a dictionary, not an array. Have you considered that as an alternative data structure?

How to do associative array/hashing in JavaScript

var myArray = {};
myArray["5"] = "something";
myArray["10"] = "nothing";

And to get the length you would want to write a quick function like the one shown here:

Length of a JavaScript object

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

var size = Object.size(myArray);

Or alternatively, even simpler (but not supported by IE8):

Object.keys(myArray).length

4 Comments

Set might make more sense, as it is a native implementation of your code (and contains a size property)
@RobM. Yeah using a Set.prototype would be a great way to solve this problem as well and would be more resource efficient.
Seeing as the indexes are probably important, I think a Map is closer. Set and Map have low browser support still though.
@4castle I was just about to comment that that, "hold on, actually set loses the key" so yeah it sounds like the keys are important so Map would be better, and the implementation above is probably more likely to be supported than Map.
0
var array = [];  
array[5] = "something";  
array[10] = "nothing": 

Your array in this case becomes :

[undefined,undefined,undefined,undefined,undefined,"something",undefined,undefined,undefined,undefined,"nothing"]

thats why the length is coming as 11. So ideally you should not set it like array[5] or array[10].

If you have variable keys, then you should use object

a = {};    
a["5"] = "something";
a["10"] = "nothing"

Then a will look like {"5":"something","10":"nothing"}

Then you can calculate count as :

var elemCount = 0;
for(var key in a){
elemCount++;
}

Output:

elemCount = 2

Various other ways of getting length of keys in object: How to efficiently count the number of keys/properties of an object in JavaScript?

Comments

-1

var array= Array(5);
array[1] = 10;
array[3] = 3;

numberOfElements = 0;

for(var i  =0; i<array.length; i++) {
	if(array[i]!=undefined)
		numberOfElements++;
}
alert(numberOfElements);

1 Comment

You should use the !== operator in case the user put nulls in the array.

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.