473,387 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

indices into an array

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

May 19 '06 #1
12 1627
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/
May 19 '06 #2
VK
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).

May 19 '06 #3
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/
May 19 '06 #4
VK

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

May 19 '06 #5
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/>
May 19 '06 #6
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/
May 19 '06 #7
JimK said the following on 5/19/2006 9:46 AM:
On Thu, 18 May 2006 23:35:01 -0400, Randy Webb
<Hi************@aol.com> wrote:


I did not write that. Please quote properly.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 19 '06 #8
JimK said the following on 5/19/2006 10:13 AM:
On Fri, 19 May 2006 13:56:22 GMT, JimK <1a****@gmail.com> wrote:
On Fri, 19 May 2006 13:46:31 GMT, JimK <1a****@gmail.com> 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])
)


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[i]]
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 19 '06 #9
JimK said the following on 5/20/2006 12:43 PM:
On Fri, 19 May 2006 15:29:46 -0400, Randy Webb
<Hi************@aol.com> wrote:
JimK said the following on 5/19/2006 10:13 AM:
On Fri, 19 May 2006 13:56:22 GMT, JimK <1a****@gmail.com> wrote:

On Fri, 19 May 2006 13:46:31 GMT, JimK <1a****@gmail.com> 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])
) 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.
}

<snip>
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.
testjs.zip
http://www.filegone.com/qdqk


Timed out.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 21 '06 #10
Randy Webb said the following on 5/20/2006 1:17 PM:
JimK said the following on 5/20/2006 12:43 PM:


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


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::

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 21 '06 #11
Andy wrote:
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
--
This is Usenet. It is a discussion group, not a helpdesk. You post
something, we discuss it. If you have a question and that happens to get
answered in the course of the discussion, then great. If not, you can
have a full refund of your membership fees. -- Mark Parnell in alt.html
May 24 '06 #12
RobG wrote:
Randy Webb wrote:
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
--
The English government is much of a German poodle as
other governments. The Germans infiltrated them all.
-- "The only real Barbara Schwarz", dsw.scientology,
<16**************************@posting.google.com >)
May 25 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: dan | last post by:
I was recently surprised, and quite shocked in fact, to find that Python treats negative indices into sequence types as if they were mod(length-of-sequence), at least up to -len(seq). This fact...
9
by: Richard A. DeVenezia | last post by:
Can someone explain why JavaScript arrays are not allowed to have objects as indices ? Would solve many a hashtable lookup problems.... -- Richard A. DeVenezia
9
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so...
3
by: Marc Schellens | last post by:
I want to get the sort indices of an valarray<type> ie. for type int and array valarray<int> a( 0, 4); a = 7, a = 2, a = 9, a = 4,
3
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C or C++ code...
26
by: Sterten | last post by:
when I define int R; and then later access it with x=R;C=7; .... but x happens to be <0 or >99 , then the program's behavious becomes unpredictable. Is there a way to prevent this ? Is...
4
by: Brian Blais | last post by:
Hello, In my attempt to learn python, migrating from matlab, I have the following problem. Here is what I want to do, (with the wrong syntax): from numpy import * t=arange(0,20,.1)...
2
by: turnips11 | last post by:
Hi All, I have an xml schema exposed through a web service and I am testing it with entering different values into 3 array indices. But when I run it it just enters the last array index values 3...
2
by: Xavier Barthelemy | last post by:
Hi all I'm becoming mad, because I can't see what's wrong: I am constructing a GUI, to plot some data. so let's have a look of what's wrong: in my code I have a variable named...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.