-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformSerialize.js
72 lines (57 loc) · 1.87 KB
/
formSerialize.js
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
62
63
64
65
66
67
68
69
70
71
72
/*
Lets serialize the following html form:
<form id="example-form">
<input type="text" name="foo.bar" value="bar"/>
<input type="text" name="foo.joe" value="baz"/>
<input type="text" name="foo.baz.bar" value="do it!"/>
</form>
into JSON in the following format:
{
foo: {
bar: "bar",
joe: "baz",
baz: {
bar: "do it!"
}
}
}
*/
const deepCopy = (sourceObj) => {
return JSON.parse(JSON.stringify(sourceObj))
}
// O(N * K); Where N is the number of input nodes and k is the maximum number of periods in the name
const serializeForm = (formId) => {
const inputs = document.querySelectorAll("input");
let serializedForm = {};
let tempObj = {};
for (let i = inputs.length-1; i >= 0; i--) {
const element = inputs[i] ;
const nameArr = element.name.split('.');
for (let j = 0; j < nameArr.length; j++) {
const name = nameArr[j];
const value = element.value;
if (!serializedForm.hasOwnProperty(name)) {
if (j===nameArr.length-1) {
serializedForm[name] = value;
} else {
}
}
}
// for (let j = nameArr.length - 1; j >= 0; j--) {
// const name = nameArr[j];
// const value = element.value;
// if (j === nameArr.length - 1) {
// serializedForm[name] = value;
// } else {
// if (!serializedForm.hasOwnProperty(name)) {
// tempObj = deepCopy(serializedForm);
// serializedForm = {};
// serializedForm[name] = tempObj;
// }
// }
// }
}
return serializedForm;
}
const result = serializeForm("example-form");
console.log(result);