console.log( $('form').serialize() );
console.log( $('form').serializeArray() );
console.log( arrayToObject( $('form').serializeArray() ) );
console.log( arrayToObject_allowmultiplevalues( $('form').serializeArray() ) );
function arrayToObject (serialized_form) {
var object = {};
for (var i = 0; i < serialized_form.length; ++i) {
object[serialized_form[i]['name']] = serialized_form[i]['value'];
}
return object;
}
function arrayToObject_allowmultiplevalues (serialized_form) {
// convert value to array if multiple values with same name.
// problem: multivalues are sometimes array and sometimes not (if only 1 value selected)
var object = {}, name, value;
for (var i = 0; i < serialized_form.length; ++i) {
name = serialized_form[i]['name'];
value = serialized_form[i]['value'];
if( object[name] === undefined) {
// 1st value, save normally:
object[name] = value;
} else if ( Array.isArray(object[name]) ) {
// already an array value for this name, push new value:
object[name].push(value);
} else {
// already a value for this name, but not an array yet. make new array with old and new value:
object[name] = [ object[name], value];
}
}
for (name in object) {
// names ending in '[]' are always considered arrays:
// (this fixes entries with only 1 or 0 values selected ending up as non-array)
if ( name.substr(-2) === '[]' && ! Array.isArray(object[name]) ) {
object[name] = [ object[name] ];
}
}
return object;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.