Sunday, July 29, 2012

Condensing a sparse array in Javascript?



I have an array of elements where the entries are sparse. How can I easily condense the sparse array into a dense array so that I don\’t have to keep checking for null and undefined values every time I loop through the data?


Here is some example data:



var sparse = [];
sparse[1] = undefined;
sparse[5] = 3;
sparse[10] = null;

var dense = sparseToDenseArray(sparse);
// dense should be [3]






Rating: 2 out of 5 based on 3 ratings



The post Condensing a sparse array in Javascript? appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/condensing-a-sparse-array-in-javascript.html

No comments:

Post a Comment