【发布时间】:2014-07-02 06:01:14
【问题描述】:
我正在为我的应用程序进行扁平化对象处理。我得到了谷歌的功能。但我无法理解该功能。有人帮帮我吗?
这是我对细节的疑问:
var object = {
"address" : {
"name" : "siva"
}
};
var flatter = function (ob) {
var newObj = {};
for(var i in ob) { //first time will be address but it will update immediately
console.log(i,'1')
if((typeof ob[i]) == 'object') {
var cObj = flatter(ob[i]); // i am calling 2nd time, but still address is exist..how?
for(var x in cObj) {
console.log(i, x,'2')
newObj[i + '.' + x] = cObj[x]; //now `i` became name, but i am getting address with name where the address label stored?
}
}
else {
newObj[i] = ob[i];
}
if (($.type(ob[i])) == 'null') {
newObj[i] = ob[i];
}
}
return newObj;
};
console.log(flatter(object)); // i am getting proper result as : address.name: "siva"
//查看现场演示。
【问题讨论】:
标签: javascript loops object