我正在尝试将所有""
值转换为false
使用下划线,但它不起作用。有没有更简单的方法?
var _ = require("underscore");
var test = {
"one": "",
"two": "",
"three": {
"four": ""
},
"five": "this string is intact"
};
第一次尝试,弄乱了物体
function z(object){
return _.map(object, function(value, key, list){
if(_.isObject(value)){
return z(value);
}else{
var ret = {};
ret[key] = (value == "") ? false : value;
return ret;
}
});
}
第二次尝试失败
var _false = function(object){
var nb = {};
var _false = function _false(object, parent){
_.each(object, function(value, key, list){
if(_.isObject(value)){
nb[key] = {};
return _false(value, key);
}else{
nb[parent] = (value == "") ? false : value;
}
});
}(object);
return nb;
}