Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #27111
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2015-03-23 10:32 -0700 |
| References | <2c9d128f-2237-4484-bbbe-2020864ec72a@googlegroups.com> <27052983-4f87-4ee8-9778-8337a84e359f@googlegroups.com> |
| Message-ID | <9541babc-1ad6-4528-a2cd-5bda67d2676a@googlegroups.com> (permalink) |
| Subject | Re: Remove leading zeros in index of array |
| From | Scott Sauyet <scott.sauyet@gmail.com> |
jonas.thornvall@gmail.com wrote:
> jonas.thornvall@gmail.com wrote:
>> I store integers in the indexes of an array, is there a prefared
>> or easy way to remove all leading indexes containing zeros?
>> [ ... ]
> This is what i've come up with.
>
> while (result[length]==0) {result.splice(length, 1);length+=-1;}
One possibility:
var removeLeadingZeros = function(list) {
var firstNonZero = 0;
list.every(function(item, index) {
if (item !== 0) {
firstNonZero = index;
return false;
}
return true;
});
return list.slice(firstNonZero);
};
This depends on `Array.prototype.every` which is not available in JScript
before Chakra (i.e. before IE9), and would have to be polyfilled if you
wanted to use it in that environment.
While it's not a beautiful solution, it certainly seems better than
continually `splice`ing out values. Note that this does *not* modify your
original list but returns a copy of it. To my mind this is an advantage;
but if you prefer, you could always replace that last line with a single
`splice` call.
-- Scott
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Remove leading zeros in index of array jonas.thornvall@gmail.com - 2015-03-23 04:57 -0700
Re: Remove leading zeros in index of array jonas.thornvall@gmail.com - 2015-03-23 10:03 -0700
Re: Remove leading zeros in index of array Scott Sauyet <scott.sauyet@gmail.com> - 2015-03-23 10:32 -0700
Re: Remove leading zeros in index of array Tim Streater <timstreater@greenbee.net> - 2015-03-23 18:36 +0000
Re: Remove leading zeros in index of array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-03-23 20:42 +0100
Re: Remove leading zeros in index of array jonas.thornvall@gmail.com - 2015-03-23 12:58 -0700
Re: Remove leading zeros in index of array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-03-23 21:12 +0100
Re: Remove leading zeros in index of array jonas.thornvall@gmail.com - 2015-03-23 13:16 -0700
Re: Remove leading zeros in index of array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-03-23 21:36 +0100
Re: Remove leading zeros in index of array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-03-23 21:49 +0100
Re: Remove leading zeros in index of array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-03-23 21:58 +0100
Re: Remove leading zeros in index of array Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2015-03-24 15:41 +0100
Re: Remove leading zeros in index of array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-24 20:21 +0100
Re: Remove leading zeros in index of array Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2015-03-24 20:45 +0100
Re: Remove leading zeros in index of array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-24 20:47 +0100
Re: Remove leading zeros in index of array Scott Sauyet <scott.sauyet@gmail.com> - 2015-03-24 13:37 -0700
Re: Remove leading zeros in index of array Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2015-03-25 00:01 +0100
csiph-web