PHP-Like associative array initialization ?

Discussion in 'Javascript' started by Remi Bastide, Jan 6, 2005.

  1. Remi Bastide

    Remi Bastide Guest

    Is is possible to initialize a javascript associative array inline, as
    you would do in PHP, e.g. :

    <?php
    $a = array("abc" => "def", "ghi" => "jkl");
    ?>
     
    Remi Bastide, Jan 6, 2005
    #1
    1. Advertisements

  2. Oops... forget about "associative arrays", as they sometimes cause
    flaming discussion down here. Just call them objects (javascript
    objects), and think of them as (imperfect) associative arrays.

    As for the initialisation syntax, which is the javascript object
    initialisation syntax ( see http://www.json.org ), you should be fine with :

    a = { "abc": "def",
    "ghi": "jkl" };

    The quotes are optionnal, so the following is fine, too:

    a= { abc: "def",
    ghi: "jkl" }

    However, if one of your keys has spaces or weird characters in it, se
    the quotes:

    cplx = { "abc d": "efg",
    hij: "klm" };

    Hope this helps,


    Alexis
     
    Alexis Nikichine, Jan 6, 2005
    #2
    1. Advertisements

  3. Remi Bastide

    Remi Bastide Guest

    Just what I needed, thanks.
     
    Remi Bastide, Jan 6, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.