Results 1 to 7 of 7

Thread: select an array value from within an array

  1. #1
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default select an array value from within an array

    There is a snip of code I am trying to optimize and have gone in a new direction. I used to use the split method to spill the contents of an array into a temporary variable as a csv file to access each individual item in that array. Now I want to create one array with many values inside as below.

    Code:
    <script type="text/javascript">
    myDB = new Array()
    item1="1";
    item2="2";
    item3="3";
    item4="4";
    
    myDB.push(item1 + "," + item2 + "," + item3 + "," + item4); // places current items inside the myDB array as a new array
    
    if (myDB[0][3]=="4") {
    alert(myDB[0][3]);
    }
    </script>
    I've picked up that the array is read as a string. What I want the script to do is treat each comma separated value as a value and to be able to directly select the value (i.e. myDB[0][0]) of the array within the array.

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there sniperman,

    I do not really understand what it is that you are trying to achieve.
    But then, I am well past my "Sell By" date.

    Nevertheless, my modification to your script appears to provide your required result....
    Code:
    
    <script type="text/javascript">
    
       myDB=new Array();
    
       item1='1';
       item2='2';
       item3='3';
       item4='4';
    
       myDB.push(item1+item2+item3+item4); 
    
    if(myDB[0][3]=='4') {
    
       alert(myDB[0][3]);
     }
    
    </script>
    
    Edit: but not in IE8, though

    coothead

  3. #3
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default

    Hi coothead

    I am trying to create a multi-dimensional array and one that is indexed.

    The basic function this script needs to replicate is the ability to look in multi-dimensional arrays for a value.

    Code:
    <script type="text/javascript">
    
       myDB=new Array();
    
       item1='1';
       item2='2';
       item3='3';
       item4='four';
    
       myDB.push(item1+item2+item3+item4); 
    
       alert(myDB[0][3]);
       
    if(myDB[0][3]=='four') {
    
       alert(myDB[0][3]);
     }
    
    </script>
    Your code works but when you throw a value with more than one character you'll see the code above returns "f" instead of "four" because the array within the array is not indexed.

    The line below seems to work

    Code:
    myDB = new Array(new Array(item1, item2,  item3, item4)) ;
    However, I am working with the push method to populate an array's node with another array, which does not seem to work on the fly.

  4. #4
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there sniperman,

    it appears that "item" is a reserved word in IE.


    Would this be acceptable to your purpose...
    Code:
    
    <script type="text/javascript">
    
       myDB=[];
       myItem=[];
    
       myDB[0]=myItem;
    
       myItem[0]='1';
       myItem[1]='2';
       myItem[2]='3';
       myItem[3]='four';
    
    if(myDB[0][3]=='four') {
    
       alert(myDB[0][3]);
    
     }
    </script>
    
    coothead

  5. The Following User Says Thank You to coothead For This Useful Post:

    sniperman (10-31-2010)

  6. #5
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default

    Yeah that's awesome coothead exactly the result I was looking for.

    I also went here to read up on arrays.

    Code:
    <script type="text/javascript">
    
       myDB=[];
       myItem=[];
    
       myDB.push(myItem);
        
    
       myItem[0]='1';
       myItem[1]='2';
       myItem[2]='3';
       myItem[3]='four';
    
    if(myDB[0][3]=='four') {
    
       alert(myDB[0][3]);
    
     }
    </script>
    I've modified the script a little bit so that the items can be dynamically inserted into the array's index, which is how the script rolls

    That optimization should remove a couple of lines of old code and make the script perform better.

    ps. I had no idea that item was a reserved word thanks for that. I'm working with Firefox so not sure whether the same problem exists.

    Sniper!

  7. #6
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    I am pleased to have helped in some small way.

  8. #7
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default

    Ok, so I'm back to punish myself with multidimensional arrays. I've taken the model above and adapted it but it needs some work to deliver the desired result.

    I need to accomplish this:

    Code:
    myDB=[];
    
    column 1: text
    column 2: value
    
          x  x 
    [0] |_|_| [0] 
    [1] |_|_| [0]
    [2] |_|_| [0]
    [3] |_|_| [0]
    [4] |_|_| [0]
    
    code is not to scale :)
    an example would be 5 objects with 1 attribute each i.e.

    Code:
    myDB=[];
    
    var rand=Math.floor(Math.random()*20;
    myDB[0] =  ["random1", rand];
    var rand=Math.floor(Math.random()*20;
    myDB[0].push("random1", rand)
    I want them to be invoked something like this

    Code:
    alert(myDB[0][0])
    should equal "random1" and "random number"

Bookmarks

Posting Permissions

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