Reputation: 3400
I have a big array (say 500 entries) but most of the values are Null. How can i represent the array instead of having [null, null ... 3, null, null, ...]
?
the array is passed to highcharts for plotting. Highcharts plots a point for every element in the array so I really don't need those null
s.
Upvotes: 2
Views: 156
Reputation: 95252
If you set array[200]
to something, it's not like JavaScript comes along behind you and sets array[0]
through array[199]
. Arrays are automatically sparse.
So is the problem that if you ask for an uninitialized element, you get undefined
back instead of null
? Or is there some other issue?
If it's the undefined
vs null
thing, then just add the null
s after you initialize it, with a loop, instead of having to put them in the big literal blob:
for (var i=0; i<array.length; ++i) {
if (typeof(array[i]) === 'undefined') {
array[i] = null;
}
}
If you want null
s out past what you initialized, replace array.length
with whatever your maximum index needs to be (+1, or else change the <
to <=
).
Upvotes: 4