PHP-Like associative array initialization ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Remi Bastide

    PHP-Like associative array initialization ?

    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");
    ?>


  • Alexis Nikichine

    #2
    Re: PHP-Like associative array initialization ?

    Remi Bastide wrote:[color=blue]
    > 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");
    > ?>
    >
    >[/color]

    Oops... forget about "associativ e 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

    Comment

    • Remi Bastide

      #3
      Re: PHP-Like associative array initialization ?

      Just what I needed, thanks.
      Alexis Nikichine <alexis.nikichi ne@somedomain.f r> wrote:
      [color=blue]
      >Remi Bastide wrote:[color=green]
      >> 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");
      >> ?>
      >>
      >>[/color]
      >
      >Oops... forget about "associativ e 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
      >initialisati on 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[/color]

      Comment

      Working...