Answer the question
In order to leave comments, you need to log in
Creating an associative array in JS?
How to create an associative array in JS inline? I honestly searched and couldn't find it.
An example of what I want in neutral PHP:
$arrArray = array("key1" => "value1", "key2" => "value2")<br/>
Interested in a short path and in one operation
Answer the question
In order to leave comments, you need to log in
<script type="text/javascript">
var arrArray1 = <?php echo json_encode($arrArray);?>;
var arrArray2 = {
key1: 'value1'
,key2: 'value2'
};
var arrArray3 = [];
arrArray3['key1'] = 'value1';
arrArray3['key2'] = 'value2';
</script>
There are no pure associative arrays in JS.
There are objects that store key-value pairs, but there are no length, pop, shift, etc. properties.
And there are arrays that do not have keys, but have all the charms of real arrays.
For anyone else , check this out , and then it 's better to read the mana on MDN .
I propose here such a crutch
//key=>value; key=>value;
function array_assoc(data){
var _arr = [];
var a = data split(";");
for(var c = 0; c < a.length; c++){
var b = a[c].split("=>");
_arr[b[0]] = b[1];
}
return_arr;
}
var my_array = array_assoc("name=>Vasya;age=16;gender=male");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question