Velocity Reviews > indices into an array

# indices into an array

Andy
Guest
Posts: n/a

 05-19-2006
Pardon the question from a beginner, but I can't seem to find the best
way to do this.

I have a large array and I would like to find all elements within it
that fall between two values. For example:
var a = new Array();
a=[1,2,3,4,5,...,100];

And say I want to find all elements of "a" that fall between 25 and 50.
A "for" loop that goes through each element of "a" just seems
inefficient. My real array is much larger than the example above
(~10,000 points). Is there a better way to do this?

Thanks,
Andy

Randy Webb
Guest
Posts: n/a

 05-19-2006
Andy said the following on 5/18/2006 11:05 PM:
> Pardon the question from a beginner, but I can't seem to find the best
> way to do this.
>
> I have a large array and I would like to find all elements within it
> that fall between two values. For example:
> var a = new Array();
> a=[1,2,3,4,5,...,100];
>
> And say I want to find all elements of "a" that fall between 25 and 50.

var begin = 25;
var end = 50;

for (var myVar = begin;myVar<end;myVar++){
if (a[myVar]){
//do something with a[myVar]
}
}

> A "for" loop that goes through each element of "a" just seems
> inefficient. My real array is much larger than the example above
> (~10,000 points). Is there a better way to do this?

Loop from begin to end instead of looping the entire array.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

VK
Guest
Posts: n/a

 05-19-2006
Andy said the following on 5/18/2006 11:05 PM:
> > And say I want to find all elements of "a" that fall between 25 and 50.

Randy Webb wrote:
> var begin = 25;
> var end = 50;
>
> for (var myVar = begin;myVar<end;myVar++){
> if (a[myVar]){
> //do something with a[myVar]
> }
> }

Wow... Splendid! (seriously - no sarcasm).

Randy Webb
Guest
Posts: n/a

 05-19-2006
VK said the following on 5/19/2006 2:27 AM:
> Andy said the following on 5/18/2006 11:05 PM:
>>> And say I want to find all elements of "a" that fall between 25 and 50.

>
> Randy Webb wrote:
>> var begin = 25;
>> var end = 50;
>>
>> for (var myVar = begin;myVar<end;myVar++){
>> if (a[myVar]){
>> //do something with a[myVar]
>> }
>> }

>
> Wow... Splendid! (seriously - no sarcasm).
>

Although it depends on what the definition of "between" is though. Does
it include or exclude 25 and 50?

Exclude them:

var begin = 25;
var end = 50;

for (var myVar = begin+1;myVar<end;myVar++){
if (a[myVar])
{
//do something with a[myVar]
}
}

Include them:

var begin = 25;
var end = 50;

for (var myVar = begin;myVar<=end;myVar++){
if (a[myVar])
{
//do something with a[myVar]
}
}

JS is simple when you don't try to make it harder than it is.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

VK
Guest
Posts: n/a

 05-19-2006

Randy Webb wrote:
> JS is simple when you don't try to make it harder than it is.

Full ACK. Did I ever say otherwise? Yet even for simple things you
need sometimes to "turn the chair you're sitting on up side down".

RobG
Guest
Posts: n/a

 05-19-2006
Randy Webb wrote:
> Andy said the following on 5/18/2006 11:05 PM:
>> Pardon the question from a beginner, but I can't seem to find the best
>> way to do this.
>>
>> I have a large array and I would like to find all elements within it
>> that fall between two values. For example:
>> var a = new Array();
>> a=[1,2,3,4,5,...,100];
>>
>> And say I want to find all elements of "a" that fall between 25 and 50.

>
> var begin = 25;
> var end = 50;
>
> for (var myVar = begin;myVar<end;myVar++){
> if (a[myVar]){

You've got to be careful with that, it tests the value of a[myVar] and
if it evaluates to false (say it's '0') than that element will be
skipped. A better test may be:

if (typeof a[myvar] != 'undefined'){

> //do something with a[myVar]
> }
> }
>
>> A "for" loop that goes through each element of "a" just seems
>> inefficient. My real array is much larger than the example above
>> (~10,000 points). Is there a better way to do this?

>
> Loop from begin to end instead of looping the entire array.
>

--
Rob
Group FAQ: <URL:http://www.jibbering.com/faq/>

Randy Webb
Guest
Posts: n/a

 05-19-2006
RobG said the following on 5/19/2006 3:30 AM:
> Randy Webb wrote:
>> Andy said the following on 5/18/2006 11:05 PM:
>>> Pardon the question from a beginner, but I can't seem to find the best
>>> way to do this.
>>>
>>> I have a large array and I would like to find all elements within it
>>> that fall between two values. For example:
>>> var a = new Array();
>>> a=[1,2,3,4,5,...,100];
>>>
>>> And say I want to find all elements of "a" that fall between 25 and 50.

>>
>> var begin = 25;
>> var end = 50;
>>
>> for (var myVar = begin;myVar<end;myVar++){
>> if (a[myVar]){

>
> You've got to be careful with that, it tests the value of a[myVar] and
> if it evaluates to false (say it's '0') than that element will be
> skipped. A better test may be:
>
> if (typeof a[myvar] != 'undefined'){
>

Very true.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

JimK
Guest
Posts: n/a

 05-19-2006
On Thu, 18 May 2006 23:35:01 -0400, Randy Webb
<(E-Mail Removed)> wrote:

> Pardon the question from a beginner, but I can't seem to find the best
> way to do this.
>
> I have a large array and I would like to find all elements within it
> that fall between two values. For example:
> var a = new Array();
> a=[1,2,3,4,5,...,100];
>
> And say I want to find all elements of "a" that fall between 25 and 50.

var myarray1 = new Array(6)
var myarray2 = new Array(0)
var begin = 25
var end1 = 50

var x
for (x=0; x< myarray1.length - 1; x++)
if (myarray1 >= begin && myarray1 <= end1)(myarray2.push(myarray1[x])
)

JimK
Guest
Posts: n/a

 05-19-2006
On Fri, 19 May 2006 13:46:31 GMT, JimK <(E-Mail Removed)> wrote:

>var myarray1 = new Array(6)
>var myarray2 = new Array(0)
>var begin = 25
>var end1 = 50
>
>var x
>for (x=0; x< myarray1.length - 1; x++)
>if (myarray1 >= begin && myarray1 <= end1)(myarray2.push(myarray1[x])
>)

var myarray1 = new Array(6)
var myarray2 = new Array(0)
var begin = 25
var end1 = 50

var x
for (x=0; x< myarray1.length; x++)
if (myarray1 >= begin && myarray1 <= end1)(myarray2.push(myarray1[x])
)

on second look you dont want - 1 after myarray1.length....

JimK
Guest
Posts: n/a

 05-19-2006
On Fri, 19 May 2006 13:56:22 GMT, JimK <(E-Mail Removed)> wrote:

>On Fri, 19 May 2006 13:46:31 GMT, JimK <(E-Mail Removed)> wrote:

var myarray1 = new Array(6)
var myarray2 = new Array(0)
var begin = 25
var end1 = 50

var x
for (x=0; x< myarray1.length; x++)
if (myarray1[x] >= begin &&
myarray1[x] <= end1)(myarray2.push(myarray1[x])
)

forgot to index the array, its early and no coffee yet