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
    121
    Thanks
    0
    Thanked 3 Times in 3 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
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 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.
    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
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,918
    Thanks
    56
    Thanked 545 Times in 542 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
    122
    Thanks
    0
    Thanked 25 Times in 25 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?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 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.
    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.


  •  

    Posting Permissions

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