SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2013
    Posts
    79
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Confusing multi-dimensional array

    var myMultiArray = [
    [1,2,3,4,5, [1,2,3,4,5] ],
    [6,7,8,9,10 , [1,2,3,4,6] ],
    [11,12,13,14,15 , [1,2,3,4,5] ],
    [16,17,18,19,20, [1,2,3,4,5] ]
    ];

    console.log( myMultiArray[1][5][4] ); //Outputs 6 , the value in the last element of the last element of the second element of myMultiArray.

    what does 1 represent?
    what does 5 represent?
    what does 4 represent?

    In a 2d multi dimensional array, the first index is row, the second index is column.

    so this one is confusing me, since it has more than 2 indexes.

    Please explain.

  2. #2
    Non-Member
    Join Date
    May 2012
    Posts
    14
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    very simple. Try one index at a time


  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2013
    Posts
    79
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    interesting approach, thanks.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,140
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    The main array has 4 members, indexed 0-3, each an array
    Each of those arrays has 6 members, indexed 0-5, the first 5 members being Ints and the last an array
    Each of the last arrays have 5 members, indexed 0-4, Ints

    The drill down [1][5][4] goes to
    second member -> sixth member -> fifth member

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2013
    Posts
    79
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    The only thing left

    Quote Originally Posted by Mittineague View Post
    The main array has 4 members, indexed 0-3, each an array
    Each of those arrays has 6 members, indexed 0-5, the first 5 members being Ints and the last an array
    Each of the last arrays have 5 members, indexed 0-4, Ints

    The drill down [1][5][4] goes to
    second member -> sixth member -> fifth member


    the other question i have is why nest an array within each array? what possible use case scenarios are there for this?

    I can't really visualize a scenario where i would use something this weird?

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,800
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    JavaScript does not support multi-dimensional arrays so the only way to create arrays with two or more dimensions is to nest arrays insode of arrays. When it comes to where you have data for a two dimensional array you would ususally set up an array for the rows and nest an array giving the values for each column inside of each row element as that makes it easiest to translate into HTML.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2013
    Posts
    79
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    JavaScript does not support multi-dimensional arrays so the only way to create arrays with two or more dimensions is to nest arrays insode of arrays. When it comes to where you have data for a two dimensional array you would ususally set up an array for the rows and nest an array giving the values for each column inside of each row element as that makes it easiest to translate into HTML.
    Can you please provide a simplified example where each row is a baseball player for example and each column is a stat like batting average, steals, and strikeouts for example?

    And i was also wondering how would multi-dimensional arrays be implemented in a regular oop language like java or C++ for example? since you say that javascript doesn't support it, i want to see the difference.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,800
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by wc43870n View Post
    since you say that javascript doesn't support it, i want to see the difference.
    In languages that support multi dimensional arrays you would reference the elements of a three dimensional array using something like myArray[2,4,6] whereas in languages that don't support multi dimensional arays where you have to nest arrays inside of other arrays to achieve the same result you'd reference it like myArray[2][4][6]. The way you define the arrays would be different as well since in languages that support multi dimensional arrays you usually need to spepecify how many entries there are in each dimension.

    With nested arrays you don't have to have the same number of entries in each dimension the way you do with multi domensional arrays. So in JavaScript with your array of baseball players there is nothing in the language that requires that you have a second array inside of each that has four entries - they can have any number.

    With the baseball example I'd actually be more inclined to use a single array where each entry is an object with four named properties:

    players = [{player:'joe',average:10,steals:28,striheouts:12},
    {player:'sam',average:12,steals:17,strikeouts:10},
    ...
    ]

    Now players[0].player = 'joe, players[1].steals = 10 etc - far easier to tell what is what rather than having to remember that [0] is the name, etc.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2013
    Posts
    79
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    players = [{player:'joe',average:10,steals:28,striheouts:12},
    {player:'sam',average:12,steals:17,strikeouts:10},
    ...
    ]
    so this is how its done in java?

    so in java, you would access a 3 dimensional array like this:

    myArray[2,4,6].

    But how would you define the 3 dimensional array and could we use baseball as the example for defining it?

    Code:
    players = [{player:'joe',average:10,steals:28,striheouts:12},
    {player:'sam',average:12,steals:17,strikeouts:10},
    the above example is only 1 dimensional? correct?

  10. #10
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wc43870n View Post
    And i was also wondering how would multidimensional arrays be implemented in a regular oop language like java or C++ for example? since you say that javascript doesn't support it, i want to see the difference.
    Java doesn't support multidimensional arrays either. myArray[2, 4, 6] is syntax sugar for arrays of arrays, like in JavaScript. BTW, what "regular OOP" got to do with this?

    Multidimensional arrays are only attractive for math intensive work, if there is also a contigous memory allocation for the array elements. Java and JavaScript don't offer this. Are you looking for this? Then go with C or other languages that allow you direct memory access.

    If you want to store structured data, try the JSON format. If you want to keep using arrays for this data, I suggest you look up stacked arrays. It involves using a formula to calculate element's indices. You have to come up with the formula for your custom arrays of arrays construction, which is but one linear array.

  11. #11
    SitePoint Enthusiast
    Join Date
    Mar 2013
    Posts
    79
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by myty View Post
    Java doesn't support multidimensional arrays either. myArray[2, 4, 6] is syntax sugar for arrays of arrays, like in JavaScript. BTW, what "regular OOP" got to do with this?

    Multidimensional arrays are only attractive for math intensive work, if there is also a contigous memory allocation for the array elements. Java and JavaScript don't offer this. Are you looking for this? Then go with C or other languages that allow you direct memory access.

    If you want to store structured data, try the JSON format. If you want to keep using arrays for this data, I suggest you look up stacked arrays. It involves using a formula to calculate element's indices. You have to come up with the formula for your custom arrays of arrays construction, which is but one linear array.
    just wanted to know more about regular oop languages for context and also comparison.

    what languages do support multi dimensional arrays.

  12. #12
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    OK, to clarify the OO thing. The strict definition of object oriented revolves around one thing: the object.

    What is an object? An object is a collection of name-value pairs properties. Some of the pairs may define data, some of the pairs may describe behavior.

    With this definition, JavaScript is certainly one of the regular oop languages.

    Things start to get blurry and arguments tend to heat up with more advance topics like inheritance, encapsulation, polymorphism. While there is a single theoretical ground for these techniques, implementations may vary, leading to endless talks. So lets end this here before it turns in another flame war.

    Your last question, in a way, is similar to the oop debate. What languages support multidimensional arrays is not important right now. They all do, but implementations may very. Instead, stop making us take shots in the dark and clarify your goal.

    Is your goal to write a powerful numbers crunching library? Then JavaScript is not for you. Neither are JavaScript arrays any help here, implicitly.

    Is your goal structured data representation and manipulation? Then JavaScript may be the one to help. What other requirements do you have, that is the question?

    My point is, there's no general answer to an unspecific question. Performance, code readability, maintenance, target platform are only a few factors that could help you and us shape a useful answer. Which is usually a pattern, but being more specific stops you from choosing a slightly inappropriate one.


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
  •