PHP-Like associative array initialization ?

R

Remi Bastide

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

Alexis Nikichine

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

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
 
R

Remi Bastide

Just what I needed, thanks.
Alexis Nikichine said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top