indices into an array

Discussion in 'Javascript' started by Andy, May 19, 2006.

  1. Andy

    Andy Guest

    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
     
    Andy, May 19, 2006
    #1
    1. Advertising

  2. Andy

    Randy Webb Guest

    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/
     
    Randy Webb, May 19, 2006
    #2
    1. Advertising

  3. Andy

    VK Guest

    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).
     
    VK, May 19, 2006
    #3
  4. Andy

    Randy Webb Guest

    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/
     
    Randy Webb, May 19, 2006
    #4
  5. Andy

    VK Guest

    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".
     
    VK, May 19, 2006
    #5
  6. Andy

    RobG Guest

    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/>
     
    RobG, May 19, 2006
    #6
  7. Andy

    Randy Webb Guest

    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/
     
    Randy Webb, May 19, 2006
    #7
  8. Andy

    JimK Guest

    On Thu, 18 May 2006 23:35:01 -0400, Randy Webb
    <> 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, May 19, 2006
    #8
  9. Andy

    JimK Guest

    On Fri, 19 May 2006 13:46:31 GMT, JimK <> 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, May 19, 2006
    #9
  10. Andy

    JimK Guest

    On Fri, 19 May 2006 13:56:22 GMT, JimK <> wrote:

    >On Fri, 19 May 2006 13:46:31 GMT, JimK <> 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
     
    JimK, May 19, 2006
    #10
  11. Andy

    Lee Guest

    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.


    --
     
    Lee, May 19, 2006
    #11
  12. Andy

    Randy Webb Guest

    Randy Webb, May 19, 2006
    #12
  13. Andy

    Randy Webb Guest

    JimK said the following on 5/19/2006 10:13 AM:
    > On Fri, 19 May 2006 13:56:22 GMT, JimK <> wrote:
    >
    >> On Fri, 19 May 2006 13:46:31 GMT, JimK <> 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]
    }

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, May 19, 2006
    #13
  14. Andy

    JimK Guest

    On Fri, 19 May 2006 15:29:46 -0400, Randy Webb
    <> wrote:

    >JimK said the following on 5/19/2006 10:13 AM:
    >> On Fri, 19 May 2006 13:56:22 GMT, JimK <> wrote:
    >>
    >>> On Fri, 19 May 2006 13:46:31 GMT, JimK <> 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.

    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]
    >}
     
    JimK, May 20, 2006
    #14
  15. Andy

    Randy Webb Guest

    JimK said the following on 5/20/2006 12:43 PM:
    > On Fri, 19 May 2006 15:29:46 -0400, Randy Webb
    > <> wrote:
    >
    >> JimK said the following on 5/19/2006 10:13 AM:
    >>> On Fri, 19 May 2006 13:56:22 GMT, JimK <> wrote:
    >>>
    >>>> On Fri, 19 May 2006 13:46:31 GMT, JimK <> 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/
     
    Randy Webb, May 20, 2006
    #15
  16. Andy

    Randy Webb Guest

    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/
     
    Randy Webb, May 20, 2006
    #16
  17. 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
     
    Thomas 'PointedEars' Lahn, May 24, 2006
    #17
  18. 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,
    <>)
     
    Thomas 'PointedEars' Lahn, May 25, 2006
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin
    Replies:
    1
    Views:
    481
    Gunnar Hjalmarsson
    Jun 3, 2004
  2. b83503104
    Replies:
    3
    Views:
    3,993
    Al Bowers
    May 21, 2004
  3. Thomas Hawtin

    64-bit array indices RFE

    Thomas Hawtin, Aug 12, 2006, in forum: Java
    Replies:
    2
    Views:
    1,326
    Patricia Shanahan
    Aug 12, 2006
  4. Sterten

    checking array indices

    Sterten, Jul 24, 2004, in forum: C Programming
    Replies:
    26
    Views:
    638
    Ricardo Gibert
    Jul 27, 2004
  5. KLaus Blank
    Replies:
    1
    Views:
    456
Loading...

Share This Page