indices into an array

A

Andy

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
 
R

Randy Webb

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.
 
V

VK

Andy said the following on 5/18/2006 11:05 PM:
Randy said:
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).
 
R

Randy Webb

VK said the following on 5/19/2006 2:27 AM:
Andy said the following on 5/18/2006 11:05 PM:
Randy said:
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.
 
V

VK

Randy said:
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".
 
R

RobG

Randy said:
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.
 
R

Randy Webb

RobG said the following on 5/19/2006 3:30 AM:
Randy said:
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.
 
J

JimK

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])
)
 
J

JimK

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....
 
J

JimK

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
 
L

Lee

Andy said:
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?

You need to be more precise in your problem specification.
You've received two different answers, one assuming that
you mean that the indices of the elements fall between 25
and 50, and the other assuming that you mean that the values
of the elements fall between 25 and 50. There have also been
two different interpretations of "between 25 and 50".

Looping through all elements of an array is not necessarily
inefficient. Inefficiency comes from doing it more often
than you need to.

If you need to extract a range of values, and you're going
to be doing this several times, it's probably more efficient
to sort the array first, so you can more efficiently find
your begin and end points.


--
 
R

Randy Webb

JimK said the following on 5/19/2006 9:46 AM:
I did not write that. Please quote properly.
 
R

Randy Webb

JimK said the following on 5/19/2006 10:13 AM:
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])
)

One thing you may want to add here is a pointer to the original array
index so that if the original array needs to be changed then you have a
pointer to where to find those elements. And for doing that, push is not
the best way to do that.

var counter = 0
if (begin<=myArray1[x]<=end1){
myArray2[counter] = x;
counter++;
}

Now, you can loop through myArray2 and modify the contents of myArray1
by using myArray1[myArray2[index]]

for (var i=0;i<myArray2.length;i++){
//modify myArray1[myArray2]
}
 
J

JimK

JimK said the following on 5/19/2006 10:13 AM:
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])
)

One thing you may want to add here is a pointer to the original array
index so that if the original array needs to be changed then you have a
pointer to where to find those elements. And for doing that, push is not
the best way to do that.

Good point on the pointer, but the OP did ask for element into 2nd
array.

Its easy enough to push the index pointer instead of the value
(anything wrong with this)

var x // Example 1
for (x=0; x< myArray1.length; x++)
if (myArray1[x] >= begin && myArray1[x] <= end1){
myArray2.push(x)
}

or push index pointer and value

var x // Example 2
for (x=0; x< myArray1.length; x++)
if (myArray1[x] >= begin && myArray1[x] <= end1){
myArray2.push([x, myArray1[x]])
}
var counter = 0
if (begin<=myArray1[x]<=end1){
myArray2[counter] = x;
counter++;
}

Actually, your code did not work

var myArray1 = new Array(6) // Code Does Not Work
var myArray2 = new Array(0)
var begin = 25
var end1 = 50

var counter = 0
var x
for (x=0; x< myArray1.length; x++)

if (begin <= myArray1[x] <= end1){ // You sure about this
myArray2[counter] = x
counter++;
}




var myArray1 = new Array(6) // But This Code Works
var myArray2 = new Array(0)
var begin = 25
var end1 = 50

var counter = 0
var x
for (x=0; x< myArray1.length; x++)

if (myArray1[x] >= begin && myArray1[x] <= end1){
myArray2[counter] = x
counter++;
}

Are you sure 'a < b < c' is valid in javascript,
testjs.zip
http://www.filegone.com/qdqk

Now, you can loop through myArray2 and modify the contents of myArray1
by using myArray1[myArray2[index]]

for (var i=0;i<myArray2.length;i++){
//modify myArray1[myArray2]
}
 
R

Randy Webb

JimK said the following on 5/20/2006 12:43 PM:
JimK said the following on 5/19/2006 10:13 AM:
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])
)
One thing you may want to add here is a pointer to the original array
index so that if the original array needs to be changed then you have a
pointer to where to find those elements. And for doing that, push is not
the best way to do that.

Good point on the pointer, but the OP did ask for element into 2nd
array.

He did? I don't see it in his original post.
Its easy enough to push the index pointer instead of the value
(anything wrong with this)

var x // Example 1
for (x=0; x< myArray1.length; x++)
if (myArray1[x] >= begin && myArray1[x] <= end1){
myArray2.push(x)
}

I don't see much point in creating a second array to have to loop
through to get to elements in the first array when you are already at
the right point in the first array.

if (myArray1[x] >= begin && myArray1[x] <= end1){
//deal with the array.
}

Are you sure 'a < b < c' is valid in javascript,

Yes, I am sure.

a = 2;
b = 3;
c = 5;
if (a<b<c)
{
alert('Its true')
}
else
{
alert('Its false')
}

Easy enough to test.

Timed out.
 
R

Randy Webb

Randy Webb said the following on 5/20/2006 1:17 PM:
JimK said the following on 5/20/2006 12:43 PM:


Yes, I am sure.

a = 2;
b = 3;
c = 5;
if (a<b<c)
{
alert('Its true')
}
else
{
alert('Its false')
}

Easy enough to test.

Egads. It isn't fun when I don't test the false part.

No, it doesn't work as I intended it to. So, you use the && that you
posted. ::sigh::
 
T

Thomas 'PointedEars' Lahn

Andy said:
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?

Not in JavaScript 1.5. As of Gecko 1.8b2, JavaScript 1.6 provides more
Array-specific methods than ECMAScript Edition 3 defines, including
Array.prototype.filter():

var filteredArray = a.filter(
function(value, index, array)
{
return (value >= 25 && value <= 50);
});

At least the for-loop is probably executed in already compiled code then,
which should be faster than a for-loop in the ECMAScript implementation.

However, the question is what do you mean by "find all elements". Do you
want the matching values, or the matching indexes? For this approach
returns an array (to be exact: a reference to an Array object) with the
matching values, ignoring their index.

<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter>


PointedEars
 
T

Thomas 'PointedEars' Lahn

RobG said:
Randy said:
Andy said the following on 5/18/2006 11:05 PM:
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'){

ACK. That almost fully backwards-compatible approach also prevents
Gecko-based UAs from displaying warnings in the error console if the
element was not defined. However, it does not exclude the possibility
that an element may be the `undefined' value. For that, the not
backwards-compatible (JavaScript 1.4+, JScript 5.0+, ECMAScript Ed. 3+)

for (var myVar = begin; myVar < end; myVar++)
{
if (myVar in a)
{
// ...
}
}

is required.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top