We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent be4122c commit 8b3153dCopy full SHA for 8b3153d
js/flatten.js
@@ -10,19 +10,29 @@ const data = {
10
},
11
c: 3
12
}
13
+/**
14
+ * 对象数据扁平化
15
+ * @param {*} obj 需要进行扁平化的对象
16
+ * @param {*} tempKey 中间key
17
+ * @param {*} resultObj 结果
18
+ */
19
function flatten(obj, tempKey, resultObj) {
20
tempKey = tempKey || '';
21
resultObj = resultObj || {};
22
+ // 遍历对象
23
for (let key in obj) {
24
var value = obj[key];
25
if (Object.prototype.toString.call(value) == '[object Object]') {
26
+ // 如果该对象的值为对象的话,那么进行递归
27
+ // 中间key加上当前对象的key和.
28
tempKey = tempKey + key + '.';
29
flatten(value, tempKey, resultObj);
30
+ // 完成之后中间key要清空
31
tempKey = '';
32
} else {
33
+ // 如果不是对象的话那么就直接将传进来的中间key加上当前key作为当前变量的key
34
resultObj[tempKey + key] = value;
35
- console.log(tempKey);
36
};
37
return resultObj;
38
0 commit comments