私はオブジェクトの配列を持っています。オブジェクトのいずれかのプロパティから「undefined」を削除したいと思います。それはまた、オブジェクトの配列を受け入れ、それから「未定義」を削除することができますので、どのように私はこれを修正することができますか?オブジェクトの配列を見て、そのプロパティから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
としない「未定義」の文字列値であるとして、次に一つの方法は、JSON.stringify
を使用することで、任意の入力
は、それは 'あなたが文字列を使用していることを意図的です'undefined'の代わりに' undefined'を使用していますか? 'null''ではなく空文字列にも設定しています(これはまだ文字列で、引用符のない' null'と同じではありません)。 – 4castle