Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #27109 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2015-03-23 04:57 -0700 |
| Last post | 2015-03-25 00:01 +0100 |
| Articles | 17 — 6 participants |
Back to article view | Back to comp.lang.javascript
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
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-23 04:57 -0700 |
| Subject | Remove leading zeros in index of array |
| Message-ID | <2c9d128f-2237-4484-bbbe-2020864ec72a@googlegroups.com> |
I store integers in the indexes of an array, is there a prefared or easy way to remove all leading indexes containing zeros? To use splice i have to loop thru the array? bin=[0,0,0,1,0,1,0,1]
[toc] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-23 10:03 -0700 |
| Message-ID | <27052983-4f87-4ee8-9778-8337a84e359f@googlegroups.com> |
| In reply to | #27109 |
Den måndag 23 mars 2015 kl. 12:58:08 UTC+1 skrev jonas.t...@gmail.com:
> I store integers in the indexes of an array, is there a prefared or easy way to remove all leading indexes containing zeros?
>
> To use splice i have to loop thru the array?
>
> bin=[0,0,0,1,0,1,0,1]
This is what i've come up with.
while (result[length]==0) {result.splice(length, 1);length+=-1;}
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2015-03-23 10:32 -0700 |
| Message-ID | <9541babc-1ad6-4528-a2cd-5bda67d2676a@googlegroups.com> |
| In reply to | #27110 |
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
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2015-03-23 18:36 +0000 |
| Message-ID | <230320151836511246%timstreater@greenbee.net> |
| In reply to | #27110 |
In article <27052983-4f87-4ee8-9778-8337a84e359f@googlegroups.com>,
<jonas.thornvall@gmail.com> wrote:
>Den måndag 23 mars 2015 kl. 12:58:08 UTC+1 skrev jonas.t...@gmail.com:
>> I store integers in the indexes of an array, is there a prefared or easy way
>> to remove all leading indexes containing zeros?
>>
>> To use splice i have to loop thru the array?
>>
>> bin=[0,0,0,1,0,1,0,1]
>
>This is what i've come up with.
>
>while (result[length]==0) {result.splice(length, 1);length+=-1;}
Or possibly:
length -= 1;
--
"People don't buy Microsoft for quality, they buy it for compatibility
with what Bob in accounting bought last year. Trace it back - they buy
Microsoft because the IBM Selectric didn't suck much" - P Seebach, afc
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-03-23 20:42 +0100 |
| Message-ID | <XnsA466D2BA96C99eejj99@194.109.6.166> |
| In reply to | #27110 |
jonas.thornvall@gmail.com wrote on 23 mrt 2015 in comp.lang.javascript:
> Den måndag 23 mars 2015 kl. 12:58:08 UTC+1 skrev jonas.t...@gmail.com:
>> I store integers in the indexes of an array, is there a prefared or easy
> way to remove all leading indexes containing zeros?
>>
>> To use splice i have to loop thru the array?
>>
>> bin=[0,0,0,1,0,1,0,1]
>
> This is what i've come up with.
>
> while (result[length]==0) {result.splice(length, 1);length+=-1;}
var a = [0,0,0,3,0,7,0,1];
while (a[0]==0)
a.splice(0, 1);
alert(a);
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-23 12:58 -0700 |
| Message-ID | <6de32517-372c-4126-8d1d-9aad9d9f592e@googlegroups.com> |
| In reply to | #27113 |
Den måndag 23 mars 2015 kl. 20:42:54 UTC+1 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 23 mrt 2015 in comp.lang.javascript:
>
> > Den måndag 23 mars 2015 kl. 12:58:08 UTC+1 skrev jonas.t...@gmail.com:
> >> I store integers in the indexes of an array, is there a prefared or easy
> > way to remove all leading indexes containing zeros?
> >>
> >> To use splice i have to loop thru the array?
> >>
> >> bin=[0,0,0,1,0,1,0,1]
> >
> > This is what i've come up with.
> >
> > while (result[length]==0) {result.splice(length, 1);length+=-1;}
>
> var a = [0,0,0,3,0,7,0,1];
>
> while (a[0]==0)
> a.splice(0, 1);
>
> alert(a);
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
Sorry Evertjan i probably wrote in the values wrong way out of head, the zeros will range from highest index.
But while
while (a[length]==0) {a.splice(length, 1);length--;}
alert(a);
while (result[length]==0) {result.splice(length, 1);length+=-1;}
Basicly same.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-03-23 21:12 +0100 |
| Message-ID | <XnsA466D7B70FBAeejj99@194.109.6.166> |
| In reply to | #27114 |
jonas.thornvall@gmail.com wrote on 23 mrt 2015 in comp.lang.javascript:
> Sorry Evertjan i probably wrote in the values wrong way out of head, the
> zeros will range from highest index.
>
> But while
> while (a[length]==0) {a.splice(length, 1);length--;}
> alert(a);
>
> while (result[length]==0) {result.splice(length, 1);length+=-1;}
> Basicly same.
Not the same, you sh/could use pop() here:
=============================
var a = [3,0,7,0,1,0,0,0]
while (a[a.length-1]==0) a.pop();
alert(a);
=============================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-23 13:16 -0700 |
| Message-ID | <9b97720c-9c4c-4b06-beb0-22fb5bb664e8@googlegroups.com> |
| In reply to | #27115 |
Den måndag 23 mars 2015 kl. 21:12:28 UTC+1 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 23 mrt 2015 in comp.lang.javascript:
>
> > Sorry Evertjan i probably wrote in the values wrong way out of head, the
> > zeros will range from highest index.
> >
> > But while
> > while (a[length]==0) {a.splice(length, 1);length--;}
> > alert(a);
> >
> > while (result[length]==0) {result.splice(length, 1);length+=-1;}
> > Basicly same.
>
> Not the same, you sh/could use pop() here:
>
> =============================
> var a = [3,0,7,0,1,0,0,0]
>
> while (a[a.length-1]==0) a.pop();
>
> alert(a);
> =============================
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
Thank you, i will try.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-03-23 21:36 +0100 |
| Message-ID | <XnsA466DBDDAD8BFeejj99@194.109.6.166> |
| In reply to | #27116 |
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);
====================
and if you want to see what is happening,
insert an alert():
while (b.pop() == 0) {alert(a.pop())};
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-03-23 21:49 +0100 |
| Message-ID | <XnsA466DE0906FE4eejj99@194.109.6.166> |
| In reply to | #27117 |
"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);
=================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-03-23 21:58 +0100 |
| Message-ID | <XnsA466DF78E9784eejj99@194.109.6.166> |
| In reply to | #27118 |
"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)
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2015-03-24 15:41 +0100 |
| Message-ID | <a2adb$55117781$6def49d7$1008@nntpswitch.blueworldhosting.com> |
| In reply to | #27109 |
On 2015-03-23 12:57, 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?
>
> To use splice i have to loop thru the array?
>
> bin=[0,0,0,1,0,1,0,1]
Maybe you can use filter:
bin = bin.filter(function(element){
if (element) {
return element;
}
return false;
});
--
Cezary Tomczyk
http://www.ctomczyk.pl/
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-24 20:21 +0100 |
| Message-ID | <1518239.EFQcgAjVos@PointedEars.de> |
| In reply to | #27124 |
Cezary Tomczyk wrote:
> bin = bin.filter(function(element){
> if (element) {
> return element;
> }
> return false;
> });
bin = bin.filter(function (e) { return e; });
<http://ecma-international.org/ecma-262/5.1/#sec-15.4.4.20>,
step 9.c.iii.
<http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.filter>,
step 11.d.iii.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2015-03-24 20:45 +0100 |
| Message-ID | <75e22$5511bec1$6def49d7$5093@nntpswitch.blueworldhosting.com> |
| In reply to | #27125 |
On 2015-03-24 20:21, Thomas 'PointedEars' Lahn wrote:
> Cezary Tomczyk wrote:
>
>> bin = bin.filter(function(element){
>> if (element) {
>> return element;
>> }
>> return false;
>> });
>
> bin = bin.filter(function (e) { return e; });
Your code is shorter then mine. However, I think it's just less readable
in my opinion.
> <http://ecma-international.org/ecma-262/5.1/#sec-15.4.4.20>,
> step 9.c.iii.
>
> <http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.filter>,
> step 11.d.iii.
Thanks for the links.
--
Cezary Tomczyk
http://www.ctomczyk.pl/
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-24 20:47 +0100 |
| Message-ID | <2204699.Ha36KrdQoR@PointedEars.de> |
| In reply to | #27126 |
Cezary Tomczyk wrote:
> On 2015-03-24 20:21, Thomas 'PointedEars' Lahn wrote:
>> Cezary Tomczyk wrote:
>>> bin = bin.filter(function(element){
>>> if (element) {
>>> return element;
>>> }
>>> return false;
>>> });
>>
>> bin = bin.filter(function (e) { return e; });
>
> Your code is shorter then mine. However, I think it's just less readable
> in my opinion.
Wait till ES 6 (or use Node.js which already supports this):
bin = bin.filter(e => e);
;-)
<https://github.com/joyent/node/wiki/ES6-%28a.k.a.-Harmony%29-Features-Implemented-in-V8-and-Available-in-Node>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2015-03-24 13:37 -0700 |
| Message-ID | <71474448-9bfe-424c-b962-aa1cb53a3256@googlegroups.com> |
| In reply to | #27124 |
Cezary Tomczyk 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?
>>
>> To use splice i have to loop thru the array?
>>
>> bin=[0,0,0,1,0,1,0,1]
>
> Maybe you can use filter:
>
> bin = bin.filter(function(element){
> if (element) {
> return element;
> }
> return false;
> });
This would result in `[1, 1, 1]` for the above input, when the OP asked
for the removal of only the _leading_ zeros: `[`1, 0, 1, 0, 1]`. (This
was amended in a later post to ask for the removal of trailing zeros,
but in either case, this technique would fail.
For leading zeros, the following should do it, returning a new array:
var i = 0; while (arr[i] == 0) {i+=1;} arr.slice(i);
And if you really want to modify the existing array, you could do this:
var i = 0; while (arr[i] == 0) {i+=1;} arr.splice(0, i);
arr;
And similarly for trailing zeros:
var i = arr.length - 1; while (arr[i] == 0) {i-=1;} arr.slice(0, i + 1);
or to modify in place:
var i = arr.length - 1; while (arr[i] == 0) {i-=1;}
arr.splice(i + 1, arr.length - i - 1);
arr;
-- Scott
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2015-03-25 00:01 +0100 |
| Message-ID | <e589a$5511ecae$6def49d7$7172@nntpswitch.blueworldhosting.com> |
| In reply to | #27128 |
On 2015-03-24 21:37, Scott Sauyet wrote:
> Cezary Tomczyk 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?
>>>
>>> To use splice i have to loop thru the array?
>>>
>>> bin=[0,0,0,1,0,1,0,1]
>>
>> Maybe you can use filter:
>>
>> bin = bin.filter(function(element){
>> if (element) {
>> return element;
>> }
>> return false;
>> });
>
> This would result in `[1, 1, 1]` for the above input, when the OP asked
> for the removal of only the _leading_ zeros: `[`1, 0, 1, 0, 1]`. (This
> was amended in a later post to ask for the removal of trailing zeros,
> but in either case, this technique would fail.
You're right. I've overlooked requirement for removal of only the
leading zeros.
> For leading zeros, the following should do it, returning a new array:
I was just experimenting (for fun) and created one line solution:
bin.toString().replace(/^(0,)+?(?=[1-9])/, '').split();
It has limits, of course, but can be extended in many ways :-)
--
Cezary Tomczyk
http://www.ctomczyk.pl/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web