【发布时间】:2015-05-21 20:40:06
【问题描述】:
所以我这几天一直在研究这个问题,但似乎找不到真正有效的答案。我正在处理一个 api(具体来说是 trello),当提供回 json 对象时,它会在没有值的属性中放置一个 NULL 值。我在从 api 获取数据后解析数据,需要一种方法来跳过空值或删除它们。例如这里是一个具有空值的对象的一部分:
{ name: 'blah',
due: null,
cards: [ [ [Object], [Object], [Object] ],
[ [Object] ],
[ [Object], [Object], [Object] ],
null,
[ [Object], [Object] ],
[ [Object], [Object], [Object], [Object] ],
[ [Object], [Object] ],
[ [Object], [Object], [Object] ] ] }
due 属性用作在别处提取的日期时间戳的覆盖。现在我正在使用这似乎工作:
if (!update.due) {
update.lastUpdated = update.history[0].date;
} else {
update.lastUpdated = update.due;
}
这是检查空值的唯一正确方法吗?这是我发现始终如一地工作的唯一方法。另外,当从对象中删除值时,有没有办法可以检查以便我可以完全删除该属性?:
"if Obj[attr] === null"
【问题讨论】:
-
我的意思是,null 总是错误的,但您可以改为检查它是否为 null
if (update.due === null),这样您就不会得到误报(或否定,但你怎么看)。
标签: javascript json node.js trello