I want to convert the following PHP code to JavaScript, This is an array of key/value pairs in PHP:
PHP code:
$countries = array("AF"=>"AFGHANISTAN","AX"=>"ALAND ISLANDS");
What's the best way to do this in jQuery or JavaScript?
I want to convert the following PHP code to JavaScript, This is an array of key/value pairs in PHP: PHP code:
What's the best way to do this in jQuery or JavaScript? |
|||||
|
Then, to get the value for a given key, use one of the following:
|
|||
|
javascript doesn't have an associative array (like in PHP, your example above), instead, we call it javascript object:
|
|||
|
Use json_encode():
That's what will be stored in
If you just want to reproduce such a data structure in JS, use objects as stated in the other posts. |
|||||
|