Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #27119
| Newsgroups | comp.lang.javascript |
|---|---|
| Subject | Re: Remove leading zeros in index of array |
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
| References | (3 earlier) <6de32517-372c-4126-8d1d-9aad9d9f592e@googlegroups.com> <XnsA466D7B70FBAeejj99@194.109.6.166> <9b97720c-9c4c-4b06-beb0-22fb5bb664e8@googlegroups.com> <XnsA466DBDDAD8BFeejj99@194.109.6.166> <XnsA466DE0906FE4eejj99@194.109.6.166> |
| Date | 2015-03-23 21:58 +0100 |
| Message-ID | <XnsA466DF78E9784eejj99@194.109.6.166> (permalink) |
"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 23 mrt 2015 in
comp.lang.javascript:
> "Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 23 mrt 2015 in
> comp.lang.javascript:
>
>> jonas.thornvall@gmail.com wrote on 23 mrt 2015 in comp.lang.javascript:
>>
>>>> ========================
>>>> var a = [3,0,7,0,1,0,0,0]
>>>>
>>>> while (a[a.length-1]==0) a.pop();
>>>>
>>>> alert(a);
>>>> ========================
>>>>
>>> Thank you, i will try.
>>
>> Since pop() returns what is popped,
>> you could try this less efficient,
>> but perhaps also enjoyable way:
>>
>> ====================
>> var a = [3,0,7,0,1,0,0,0];
>> var b = a.slice(); // copy array a to b
>>
>> while (b.pop() == 0) {a.pop()};
>>
>> alert(a);
>> ====================
>
> Or push the popped nonzero element back:
>
> =================
> var a = [3,0,7,0,1,0,0,0], temp;
>
> while ( (temp = a.pop()) == 0) {};
>
> a.push(temp);
>
> alert(a);
> =================
Or you could use regex,
if the values are single digits:
===============
var a = [1,0,4,0,1,0,0,0];
a = a.join('').replace(/0*$/,'').split('');
alert(a);
===============
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
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