我有一个看起来像这样的对象:
宠物表
{
name:"Bobo",
type:"Golden Retriever",
food:null,
toys:null,
....
}
我想将null值的字段替换为一个空字符串,如下所示:
结果:
{
name:"Bobo",
type:"Golden Retriever",
food:"",
toys:"",
....
}
我做了以下工作:
Object.keys(PetForm).forEach((key) => (PetForm[key] === null) && PetForm[key] == "");
我在这种方法中缺少什么吗?
最佳答案
var petForm = {
name: "Bobo",
type: "Golden Retriever",
food: null,
toys: null
}
Object.keys(petForm).forEach(function(item) {
if (petForm[item] === null) {
petForm[item] = "";
}
})
console.log(petForm)