SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multidimensional array in JavaScript

    Hi,

    I am not sure if what I am trying to do is possible with JS but I hope it is. I have an array like the following in PHP and I am trying to construct it in JavaScript.

    PHP Code:
    <?php
    $myArray 
    = array(
        
    'part1' => array('id' => 75'name' => 'blue_part'),
        
    'part2' => array('id' => 14'name' => 'green_part'),
        
    'part3' => array('id' => 40'name' => 'red_part'),
    );
    ?>
    I know how to create a basic array in JS like the following:

    HTML Code:
    var myArray = new Array('part1', 'part2', 'part3');
    How can I add the multidimensionality to the JS array?

    Thanks for all ideas.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,804
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The JavaScript equivalent is:

    Code:
    $myArray = {
        'part1' : {'id' : 75, 'name' : 'blue_part'}, 
        'part2' : {'id' : 14, 'name' : 'green_part'}, 
        'part3' : {'id' : 40, 'name' : 'red_part'}, 
    };
    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="^$">

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    The JavaScript equivalent is:

    Code:
    $myArray = {
        'part1' : {'id' : 75, 'name' : 'blue_part'}, 
        'part2' : {'id' : 14, 'name' : 'green_part'}, 
        'part3' : {'id' : 40, 'name' : 'red_part'}, 
    };
    Thank you.


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
  •