我有一個對象數組,我想從任何對象的任何屬性中刪除'undefined'。查看對象數組並從中刪除未定義的屬性
從對象,我用這個方法去除不確定的,
removeNullorUndefined:function(model) {
function recursiveFix(o) {
// loop through each property in the provided value
for (var k in o) {
// make sure the value owns the key
if (o.hasOwnProperty(k)) {
if (o[k] === 'undefined') {
// if the value is undefined, set it to 'null'
o[k] = '';
} else if (typeof (o[k]) !== 'string' && o[k].length > 0) {
// if there are sub-keys, make a recursive call
recursiveFix(o[k]);
}
}
}
}
var cloned = $.extend(true, {}, model);
recursiveFix(cloned);
return cloned;
},
我怎麼能修改此所以它也可以接受對象的數組,並從中取出「未定義」?
欣賞任何輸入
是不是故意的,你使用一個字符串' 'undefined''而不是一個文字'undefined'?你也將它設置爲一個空字符串,而不是'null'(它仍然是一個字符串,與不帶'null的不帶引號相同) – 4castle