I need help with some arrays in JavaScript.
What I would like to do is something like this:
data=new array(
key => value,
key2 => value2,
key3 => value3,
...)
I don't know if its possible to do it in a similar way as you would do it in PHP.
I need to assign multiples key and values from a string in just one code line; I mean, in a simple way without using some kind of loop or something like that.
I'm trying without success with:
valor = "{'name':'facu','id':'12434'}";
ar = eval(valor);
name = ar['name'];
dni = ar['id'];
Edit
Thanks the people who answer, the problem is that the value of my var valor
is assigned by a string returned by ajax... what I have is that, and in data I will receive a string like "{'name':'facu','id':'12434'}"
, so I need to assign that info to an array.
$.get("gestorAjax.php", { funcion: 'getAfiliado', legAfiliado: legAfiliado} , function(data){
valor = eval(data)
});
getJSON
instead ofget
. Then jQuery will decode the JSON data for you. – bažmegakapa May 6 '12 at 18:32