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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    151
    Thanks
    0
    Thanked 4 Times in 4 Posts

    get position of an array inside of another array

    I want to get an array position inside of another array, how can i do this?

    ex:
    var i=0;

    array1=['content','content2','content3'];
    array2=['content4','conten5'];

    var array=[
    array1,
    array2
    ];

    lets say I want array1 but the second spot in the array

    how would i go about using array[] getting that specific position?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,353
    Thanks
    83
    Thanked 4,904 Times in 4,865 Posts
    I think you need to give us a real example, rather than a dummy one.

    I strongly suspect that your third array (named array) is not the best choice of structure.
    Be yourself. No one else is as qualified.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,081
    Thanks
    56
    Thanked 571 Times in 568 Posts
    Code:
    array[0][1]
    but I agree - if you describe what you are trying to do you will almost certainly get a suggestion for a better way to do it.

  • #4
    Regular Coder
    Join Date
    Aug 2012
    Posts
    154
    Thanks
    0
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by xelawho View Post
    Code:
    array[0][1]
    but I agree - if you describe what you are trying to do you will almost certainly get a suggestion for a better way to do it.

    Seems like what xelawho said, sounds like a 2-dimensional array:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    var arr = [1,2,3];
    var arr2 = ['a','b','c'];
    
    var sample = new Array(arr,arr2);
    alert(sample[1][1]); //results with 'b'
    alert(sample[0][2]);//results with 3;
    
    </script>
    </head>
    <body>
    </body>
    </html>
    1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for? Jesus is returning very soon

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,353
    Thanks
    83
    Thanked 4,904 Times in 4,865 Posts
    Yes, but my point (and Xelawho agrees) is that it's quite possible--and I think likely--that an array of arrays may not be the right way to go, depending on what he is trying to accomplish.

    I'm betting that a collection of arrays, for example, might be better.

    example:
    Code:
    var data = {
       "adam" : ['content','content2','content3'],
       "eve"  : ['content4','conten5']
    };
    Or perhaps even:
    Code:
    var people = {
       Adam : { gender : "Male", order : "First", lost : "A rib" },
       Eve  : { gender : "Female", temptedby : "Snake", order : "Second" }
    };
    Often, arrays are clumsy in comparison.


    But we won't know until/unless pdiddles posts again.
    Be yourself. No one else is as qualified.


  •  

    Posting Permissions

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