Skip to content

Commit 8b3153d

Browse files
committed
feat: add comment to flatten
1 parent be4122c commit 8b3153d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

js/flatten.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,29 @@ const data = {
1010
},
1111
c: 3
1212
}
13+
/**
14+
* 对象数据扁平化
15+
* @param {*} obj 需要进行扁平化的对象
16+
* @param {*} tempKey 中间key
17+
* @param {*} resultObj 结果
18+
*/
1319
function flatten(obj, tempKey, resultObj) {
1420
tempKey = tempKey || '';
1521
resultObj = resultObj || {};
22+
// 遍历对象
1623
for (let key in obj) {
1724
var value = obj[key];
1825
if (Object.prototype.toString.call(value) == '[object Object]') {
26+
// 如果该对象的值为对象的话,那么进行递归
27+
// 中间key加上当前对象的key和.
1928
tempKey = tempKey + key + '.';
2029
flatten(value, tempKey, resultObj);
30+
// 完成之后中间key要清空
2131
tempKey = '';
2232
} else {
33+
// 如果不是对象的话那么就直接将传进来的中间key加上当前key作为当前变量的key
2334
resultObj[tempKey + key] = value;
2435
}
25-
console.log(tempKey);
2636
};
2737
return resultObj;
2838
}

0 commit comments

Comments
 (0)