[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Remove leading zeros in index of array

JT

3/23/2015 11:58:00 AM

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]
16 Answers

JT

3/23/2015 5:03:00 PM

0

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;}

Scott Sauyet

3/23/2015 5:33:00 PM

0

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

Tim Streater

3/23/2015 6:37:00 PM

0

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

Evertjan.

3/23/2015 7:43:00 PM

0

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)

JT

3/23/2015 7:58:00 PM

0

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.

Evertjan.

3/23/2015 8:12:00 PM

0

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)

JT

3/23/2015 8:16:00 PM

0

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.

Evertjan.

3/23/2015 8:37:00 PM

0

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)

Evertjan.

3/23/2015 8:50:00 PM

0

"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)

Evertjan.

3/23/2015 8:58:00 PM

0

"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)