Reputation: 4285
i am relatively new in Javascript and jquery.
I have create an array of object using serializeArray() ,
var form_data = $("some_id").serializeArray();
where form_data returns data in the folliwing format,
[obj, obj, obj, obj]
where each obj contains data in this structure,
0: object
"name": "hotel_id"
"value": "1"
but i want it to be return in the following format,
{"hotel_id": "1"}
to do so i have tried the following code initially to either return the name
or the values
var myArray = $.map(form_data, function(element) {
return element.value;
});
its only returning the values
in this way,
["1"]
how can i return the result in {"name": "value"} pair.
Upvotes: 0
Views: 67
Reputation: 23859
Did you try the following?
var myArray = $.map(form_data, function(element) {
var elem = {};
elem[element.name] = element.value;
return elem;
});
Upvotes: 0
Reputation: 133403
You are so close, create object with property and return it.
var myArray = $.map(form_data, function(element) {
var ob = {}; //Create object
ob[element.name] = element.value; //Set element property
return ob;
});
Upvotes: 1