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.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help regarding data retrieval from Javascript Array

    Need help regarding data retrieval from Javascript Array

    Hi,

    I am new to Javascript and I have some difficulties regarding data retrieval on multi-dimensional array using Javascript.

    This is my list of array:

    Code:
    var myArray= new Array(4);
    
    
    myArray[0]= new Array(7);
    myArray[0][0] = "6";
    myArray[0][1] = "Billy";
    myArray[0][2] = "Shipping";
    myArray[0][3] = "great idea never sleeps";
    myArray[0][4] = "41";
    myArray[0][5] = "actual_264.jpg";
    myArray[0][6] = "thumbnail_364.jpg";
    
    
    myArray[1]= new Array(7);
    myArray[1][0] = "1";
    myArray[1][1] = "John";
    myArray[1][2] = "Mail Order";
    myArray[1][3] = "Awesome shopper.";
    myArray[1][4] = "22";
    myArray[1][5] = "actual_596.jpg";
    myArray[1][6] = "thumbnail_696.jpg";
    
    
    myArray[2]= new Array(7);
    myArray[2][0] = "2";
    myArray[2][1] = "Daniel";
    myArray[2][2] = "Cash Payment";
    myArray[2][3] = "Love to visit blogshops";
    myArray[2][4] = "24";
    myArray[2][5] = "actual_138.jpg";
    myArray[2][6] = "thumbnail_238.jpg";
    
    
    myArray[3]= new Array(7);
    myArray[3][0] = "3";
    myArray[3][1] = "Mary";
    myArray[3][2] = "Shipping";
    myArray[3][3] = "Find within oneself";
    myArray[3][4] = "52";
    myArray[3][5] = "actual_298.jpg";
    myArray[3][6] = "thumbnail_398.jpg";
    Is there a way I could sort the data being retrieved back such as, I wanted to loop the information such as

    Code:
    for(var i=0; i < myArray.length; i++){
    //where “Shipping” is a variable by user
    If (myArray[i][2] == "Shipping") {
    //how could I write it to display all results with the users who have shipping
    }
    If I were to write it in a SQL statement, it would be something like,
    SELECT * FROM myDatabase WHERE myArray[i][2] = 'Shipping'

    Thanks.
    Last edited by maxkohbb; 12-23-2010 at 11:10 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    You're going to love how easy this is:
    Code:
    for(var i=0; i < myArray.length; i++)
    {
        if (myArray[i][2] == "Shipping") /* if MUST be all lower case */
        {
            document.write( myArray[i] + "<hr>" );
        }
    }
    By default, when you ask to convert an array to a string, JS outputs all the elements separated by commas.

    If you want it fancier than that, you could (for example) do:
    Code:
    for(var i=0; i < myArray.length; i++)
    {
        if (myArray[i][2] == "Shipping") /* if MUST be all lower case */
        {
            document.write( myArray[i].join("<br>") + "<hr>" );
        }
    }
    Or variations on that.

    CAUTION: You can't use document.write once a page has been loaded!! I hope you knew that. You'd have to dump the data into, say, the innerHTML of some <div>.

    Or you could do all sorts of things. Such as create a <table> with one <tr> row per matched record. Or or or or...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Works perfectly!
    Thank you so much!


  •  

    Tags for this Thread

    Posting Permissions

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