I want to create an associative array in jQuery using the values returned in a JSON object. The JSON object is dynamically created:
[{"name":"key1","value":"value1"},{"name":"key2","value":"value2"},{"name":"key3","value":"value3"},{"name":"key4","value":"value4"}]
I want to create an associative array of this format using the values returned in JSON:
aResult = {key1 : 'value1', key2 : 'value2', key3 : 'value3', key4 : 'value4'};
Currently when I iterate through the JSON object, I can see the desired array structure in console
$.each(jData, function(k, v) {
if (v.name.toLowerCase().indexOf("answer") >= 0) {
name = v.name;
value = v.value;
console.log(name + ' : ' + value); //returns the structure I wish
};
});
But when I add this code in the loop to create array
var aResult = {name:value}
It returns [object Object]
What am I missing? How should I go forward? Any help is appreciated.
aResult={};aResult[name]=value;or something like that.