0
我想遍歷對象的數組,但我從一個REST API服務,有時物體的嵌套數組的父對象將丟失,例如:驗證嵌套對象的存在
var data = [
{
"post": {
"message": "this is a test",
"comments": [
{
"comment_text": "this is a comment"
}
]
}
}
]
如果我要遍歷的意見,但沒有保證的post
或comment
對象將是目前我目前使用的預驗證:
if (data && data.post && data.post.comments) {
//iteration
}
是否有這樣做的確認部分的更清潔的方式?