回答編集履歴

1

おまけの追加

2019/09/13 03:01

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
@@ -57,3 +57,59 @@
57
57
  };
58
58
 
59
59
  ```
60
+
61
+
62
+
63
+ おまけ(`reduce`を使う版)
64
+
65
+ ```Javascript
66
+
67
+ $.fn.serializeModelHumanAddress = function () {
68
+
69
+ return model.reduce((cur, item) => {
70
+
71
+
72
+
73
+ var item_name = item.name; // HUMAN[0].NAME
74
+
75
+ var item_split1 = item_name.split('['); // ['HUMAN', '0].NAME']
76
+
77
+ var item_split2 = item_split1[1].split(']'); // ['0', '.NAME']
78
+
79
+ var item_split3 = item_split2[1].split('.'); // ['.', 'NAME']
80
+
81
+
82
+
83
+ model_name = item_split1[0]; // HUMAN
84
+
85
+ model_level = item_split2[0]; // 0
86
+
87
+ prop_name = item_split3[1]; // NAME
88
+
89
+
90
+
91
+ if (!cur[model_name][model_level]) {
92
+
93
+ cur[model_name][model_level] = {};
94
+
95
+ }
96
+
97
+ cur[model_name][model_level][prop_name] = item.value;
98
+
99
+
100
+
101
+ return cur;
102
+
103
+ },
104
+
105
+ {
106
+
107
+ HUMAN: [],
108
+
109
+ ADDRESS: []
110
+
111
+ });
112
+
113
+ };
114
+
115
+ ```