forked from jhogendorn/jQuery-serializeFullArray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.serializefullarray.js
More file actions
61 lines (49 loc) · 1.46 KB
/
jquery.serializefullarray.js
File metadata and controls
61 lines (49 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*!
* jQuery serializeFullArray - v0.1 - 28/06/2010
* http://github.com/jhogendorn/jQuery-serializeFullArray/
*
* Copyright (c) 2010 Joshua Hogendorn
*
*
* Whereas .serializeArray() serializes a form into a key:pair array, .serializeFullArray()
* builds it into a n-tier object, respecting form input arrays.
*
*/
(function($){
'$:nomunge'; // Used by YUI compressor.
$.fn.serializeFullArray = function () {
// Grab a set of name:value pairs from the form dom.
var set = $(this).serializeArray();
var output = {};
for (var field in set)
{
if(!set.hasOwnProperty(field)) continue;
// Split up the field names into array tiers
var parts = set[field].name
.split(/\]|\[/);
// Start ref out at the root of the output object
var ref = output;
for (var segment in parts)
{
if(parts[segment] == '') continue;
if(!parts.hasOwnProperty(segment)) continue;
// set key for ease of use.
var key = parts[segment];
var value = {};
// If we're at the last part, the value comes from the original array.
if (segment == parts.length - 1)
{
var value = set[field].value;
}
// Create a throwaway object to merge into output.
var objNew = {};
objNew[key] = value;
// Extend output with our temp object at the depth specified by ref.
$.extend(true, ref, objNew);
// Reassign ref to point to this tier, so the next loop can extend it.
ref = ref[key];
}
}
return output;
};
})(jQuery);