0

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.

asked Jun 11, 2017 at 10:23
2
  • 1
    Could you provide the JSON you are working with? Commented Jun 11, 2017 at 10:26
  • @MinistryofChaps what is there to provide? It is well documented in serializeArray() docs which always returns same structure Commented Jun 11, 2017 at 10:29

2 Answers 2

1

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; 
});
answered Jun 11, 2017 at 10:26
Sign up to request clarification or add additional context in comments.

Comments

0

Did you try the following?

var myArray = $.map(form_data, function(element) { 
 var elem = {};
 elem[element.name] = element.value;
 return elem;
});
answered Jun 11, 2017 at 10:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.