mikelplhts
mikelplhts

Reputation: 1221

Array length with direct insert

I have an array:

var items = new Array();

when I try to do something like:

items[100] = ...

where 100 is an id. If then I go to print the length of the array I understand that it is big 100. Is there a way to do such a thing, but having the length to those items?

For example, if I have:

var items = new Array();
items[100] = item100;
items[120] = item120;
items[150] = item150;

I would like the length of the array is 3 and not 150.

Upvotes: 2

Views: 111

Answers (1)

isvforall
isvforall

Reputation: 8926

With some small manipulation you can get the real size of array

items.filter(Object).length;

But as @charlietfl said:

it seems you are trying to solve it using the wrong data structure...

Upvotes: 2

Related Questions