Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Regular Coder
    Join Date
    Nov 2008
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get arrays less than array number

    I have 10 000 arrays in various array numbers like this

    myArray[0] = "Football";
    myArray[3] = "Baseball";
    myArray[5] = "Cricket";
    myArray[9] = "Football";
    myArray[17] = "Baseball";
    myArray[2289] = "Cricket";
    myArray[739] = "Football";
    myArray[235] = "Baseball";
    myArray[13] = "Cricket";

    How can I find out if there is any array less than myArray[500]?

    And if i can find it out ... can I automatically list all those arrays in another array for example? Because I want to delete all arrays less than myArray[500] without having to loop. because when I have a million arrays .. it will take too long.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by gani View Post
    How can I find out if there is any array less than myArray[500]?

    And if i can find it out ... can I automatically list all those arrays in another array for example?
    Maybe use this demo as a guide

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
            <title></title>
            <style type="text/css"></style>
            <script type="text/javascript">
                var outArray = new Array();
                var myArray = new Array();
                myArray[0] = "Football";
                myArray[3] = "Baseball";
                myArray[5] = "Cricket";
                myArray[9] = "Football";
                myArray[17] = "Baseball";
                myArray[2289] = "Cricket";
                myArray[739] = "Football";
                myArray[235] = "Baseball";
                myArray[13] = "Cricket";
                for(var i in myArray){
                    if(i < 500){outArray.push(myArray[i]);}
                }
                //for testing purposes output outArray
                var str = '';
                for(i=0; i < outArray.length; i++){
                    str += outArray[i]+"\n";
                }
                alert(str);
            </script>
        </head>
        <body>
    
        </body>
    </html>

  • #3
    Regular Coder
    Join Date
    Nov 2008
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    looks like what i need. However, excuse the ignorance but doesn't

    Code:
    for(var i in myArray)
    loop through all the arrays elements?

    if so, then it is not good because if there are 1000 000 elements it will be too much on the client computer.

  • #4
    Regular Coder oVTech's Avatar
    Join Date
    Nov 2010
    Location
    USA
    Posts
    296
    Thanks
    4
    Thanked 54 Times in 52 Posts
    Quote Originally Posted by gani View Post
    looks like what i need. However, excuse the ignorance but doesn't

    Code:
    for(var i in myArray)
    loop through all the arrays elements?

    if so, then it is not good because if there are 1000 000 elements it will be too much on the client computer.

    I don't think that people do that kind of thing on the client. Maybe you should look into PHP and AJAX for dealing with a million array items or arrays.




    I don't know, I don't care, and it doesn't make any difference!
    -Albert Einstein-




  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by gani View Post
    looks like what i need. However, excuse the ignorance but doesn't

    Code:
    for(var i in myArray)
    loop through all the arrays elements?

    if so, then it is not good because if there are 1000 000 elements it will be too much on the client computer.
    You make a confusion between Arrays and Objects.

    An Array is an ordered list of values. Circling through the elements of an array is to be made using an incrementing loop, a simple for or an while loop

    An Object is an unordered list of property:value pairs. Circling through the properties of an Object is to be made using a for/in loop.

    Code:
    var myArray=[ // this is an Array
    'John Doe',
    34
    ]
    for(var i=0;i<myArray.length;i++){
    alert(myArray[i]);
    }
    var myObject={  // this is an Object
    'name':'John Doe',
    'age':34
    }
    for(property in myObject){
    alert(property+' : '+myObject[property])
    }
    And, if there are 1.000.000 or elements, that means you should reconsider the way you input the data. 1 million is too much even for a server-side language or an ordinary DBMS.
    Last edited by Kor; 05-16-2011 at 03:01 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Regular Coder
    Join Date
    Nov 2008
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    unfortunately it must be on clientside

    So you think there is no way to do something like

    Code:
    Get all myArray where arrayElementNumber < 500

  • #7
    Regular Coder
    Join Date
    Nov 2008
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok then ...I might as well explain what I am trying to do...

    I am dynamically creating and deleting many divs on a website. Each div has a separate id starting from 1 to 1000 000. Of course, I will not have a million div on the site. i will only have 100 or so at a time.... The problem is, the user is the one who decides which div, from 1 to 1000 000 is going to appear on the screen. So they can randomly pick 100 divs. Before I create those, i have to delete all divs that were there before.



    any ideas ?

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Simply set an upper limit of the loop increment
    Code:
    var limit=500;
    for(var i=0;i<limit;i++){
    // do whichever with myArray[i]
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by gani View Post
    ok then ...I might as well explain what I am trying to do...

    I am dynamically creating and deleting many divs on a website. Each div has a separate id starting from 1 to 1000 000. Of course, I will not have a million div on the site. i will only have 100 or so at a time.... The problem is, the user is the one who decides which div, from 1 to 1000 000 is going to appear on the screen. So they can randomly pick 100 divs. Before I create those, i have to delete all divs that were there before.



    any ideas ?
    As I said 1000000 in a row is much too much. Find a way to split, organize and systematize data.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by gani View Post
    looks like what i need. However, excuse the ignorance but doesn't

    Code:
    for(var i in myArray)
    loop through all the arrays elements?

    if so, then it is not good because if there are 1000 000 elements it will be too much on the client computer.
    My understanding of the the way the loop works is that it only loops through the actual elements in the array. So even if there are potentially 1,000,000 elements but only 9 actually exist as in your code then only 9 elements are looped through and the ones that meet the test condition in the IF statement are then pushed to the output array.

  • #11
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    OK, if you do not really have 1.000.000, but you want to use numbers from 0 to 1.000.000, you should definitely use an Object, not an Array. As bullant said, if you define just a single array's element as myArray[999999], myArray will still have 1000000 of elements, from which 999999 will be null.
    Code:
    var myObject= {
    '0':"Football",
    '3':"Baseball",
    '5':"Cricket",
    '9':"Football",
    '17':"Baseball" // and so on - note: the last elements must not have a comma delimiter, IE<9 fix
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #12
    Regular Coder
    Join Date
    Nov 2008
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh... i did not know that .... I thought I could just say myArray[999999] and have just one element in the array

    So I guess I am back to square one then.... I need to use an object....
    But now how will i find the records less than 500 using objects ....

    Last question guys ...promise

  • #13
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    On the other hand, if your array given as example is real, why don't you write the array in another way?
    Code:
    var myArray=[
    ['Footbal',[0,9,739]],
    ['Baseball',[3,17,235]],
    ['Cricket',[5,2289,13]]
    ]
    Still I guess that an Object is the proper solution...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #14
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by gani View Post
    But now how will i find the records less than 500 using objects ....
    Code:
    var myObject= {
    '0':"Football",
    '3':"Baseball",
    '5':"Cricket",
    '9':"Football",
    '17':"Baseball",
    '2289':"Cricket",
    '739':"Football",
    '235':"Baseball", 
    '13':"Cricket"
    }
    var limit=500;
    var outObject={};
    var a;
    for(a in myObject){
    Number(a)>limit?outObject[a]=myObject[a]:null;
    }
    //test:
    for(a in outObject){
    alert(a+' : '+outObject[a]);
    }
    Last edited by Kor; 05-16-2011 at 04:08 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #15
    Regular Coder
    Join Date
    Nov 2008
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks a million .. just what I need

    Much appreciated


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •