如何检查数组中数组中数组中的变量是否已定义且不为null?

时间:2016-05-18 09:55:11

标签: javascript

我正在尝试检查wordDefinitionId是否已定义,而不是null。这是我一直试图做的事情,但想想即使这段代码似乎也会出现一些问题。我是否有一种简单的方法可以执行此检查并将wos.wordDefinitionId的值设置为值(如果存在)或零(如果不存在)。

if (wos.word.wordForms) {
   if (wos.word.wordForms[0].wordDefinitions) {
      if (wos.word.wordForms[0].wordDefinitions[0].wordDefinitionId)
         wos.wordDefinitionId = wos.word.wordForms[0].wordDefinitions[0].wordDefinitionId
      }
   }
}

请注意,如果设置,那么我想得到wordDefinitions,它位于wordDefinitions等的第一个数组位置。

更新

我尝试了建议的答案:

if (wos.word.wordForms && wos.word.wordForms[0].wordDefinitions && wos.word.wordForms[0].wordDefinitions[0].wordDefinitionId)
    wos.wordDefinitionId = wos.word.wordForms[0].wordDefinitions[0].wordDefinitionId;
if (wos.word.wordForms && wos.word.wordForms[0].synonyms && wos.word.wordForms[0].synonyms[0].synonymId)
    wos.synonymId = wos.word.wordForms[0].synonyms[0].synonymId

// When I debug the code does not reach the next line. 

if (wos.word.wordForms && wos.word.wordForms[0].sampleSentences && wos.word.wordForms[0].sampleSentences[0].sampleSentenceId)
    wos.sampleSentenceId = wos.word.wordForms[0].sampleSentences[0].sampleSentenceId

然而,当我调试代码时没有到达最终"如果"

此处供参考:

console.log(JSON.stringify(wos.word))
VM6085:1 {"wordId":"tyyyyyy","wordIdentity":160,"ascii":116,"categoryId":1,"groupId":1,"lessonId":1,"ielts":null,"toefl":true,"toeic":null,"wordForms":[{"wordFormId":"qwqwqwqwq","wordFormIdentity":145,"ascii":113,"wordId":"tyyyyyy","primary":false,"posId":1,"sampleSentences":[],"synonyms":[],"wordDefinitions":[{"wordDefinitionId":142,"wordFormId":"qwqwqwqwq","text":"wrwrwrwrwr","ascii":119}],"pos":null,"version":"AAAAAAAADn0=","createdBy":2,"createdDate":"2016-05-03T13:23Z","modifiedBy":2,"modifiedDate":"2016-05-03T20:23Z"}],"lesson":null,"wordCategory":null,"wordGroup":null,"version":"AAAAAAAADf4=","createdBy":2,"createdDate":"2016-05-03T13:23Z","modifiedBy":2,"modifiedDate":"2016-05-03T20:23Z","current":true}

同样在这里,但也许更容易看到:

的console.log(JSON.stringify(wos.word)) VM6085:1 {" wordId":" tyyyyyy"," wordIdentity":160," ascii":116," categoryId&# 34;:1,"的groupId":1," lessonId":1," IELTS":空," TOEFL":真,& #34; TOEIC":空,"词形等":[{" wordFormId":" qwqwqwqwq"" wordFormIdentity":145, " ASCII" 113"的wordID":" tyyyyyy""主":假," posId":1 " sampleSentences":[],"同义词":[]," wordDefinitions":[{" wordDefinitionId" 142&#34 ; wordFormId":" qwqwqwqwq""文本":" wrwrwrwrwr"" ASCII" 119}]," POS":空,"版本":" AAAAAAAADn0 ="" createdBy":2" createdDate":&#34 ; 2016-05-03T13:23Z"" modifiedBy":2" modifiedDate":" 2016-05-03T20:23Z"}],& #34;教训":空," wordCategory":空," wordGroup":空,"版本":" AAAAAAAADf4 =&#34 ;," createdBy":2" createdDate":" 2016-05- 03T13:23Z"" modifiedBy":2" modifiedDate":" 2016-05-03T20:23Z""电流" :真}

2 个答案:

答案 0 :(得分:1)

在条件之间使用&&(AND) logical operator,如果第一个条件失败,则第二个条件不会检查,等等

if (wos.word.wordForms && wos.word.wordForms[0].wordDefinitions && wos.word.wordForms[0].wordDefinitions[0].wordDefinitionId)
  wos.wordDefinitionId = wos.word.wordForms[0].wordDefinitions[0].wordDefinitionId



var wos = {
  word: {
    "wordId": "tyyyyyy",
    "wordIdentity": 160,
    "ascii": 116,
    "categoryId": 1,
    "groupId": 1,
    "lessonId": 1,
    "ielts": null,
    "toefl": true,
    "toeic": null,
    "wordForms": [{
      "wordFormId": "qwqwqwqwq",
      "wordFormIdentity": 145,
      "ascii": 113,
      "wordId": "tyyyyyy",
      "primary": false,
      "posId": 1,
      "sampleSentences": [],
      "synonyms": [],
      "wordDefinitions": [{
        "wordDefinitionId": 142,
        "wordFormId": "qwqwqwqwq",
        "text": "wrwrwrwrwr",
        "ascii": 119
      }],
      "pos": null,
      "version": "AAAAAAAADn0=",
      "createdBy": 2,
      "createdDate": "2016-05-03T13:23Z",
      "modifiedBy": 2,
      "modifiedDate": "2016-05-03T20:23Z"
    }],
    "lesson": null,
    "wordCategory": null,
    "wordGroup": null,
    "version": "AAAAAAAADf4=",
    "createdBy": 2,
    "createdDate": "2016-05-03T13:23Z",
    "modifiedBy": 2,
    "modifiedDate": "2016-05-03T20:23Z",
    "current": true
  }
};



if (wos.word.wordForms && wos.word.wordForms[0].wordDefinitions && wos.word.wordForms[0].wordDefinitions[0].wordDefinitionId)
  wos.wordDefinitionId = wos.word.wordForms[0].wordDefinitions[0].wordDefinitionId

document.write('<pre>' + JSON.stringify(wos, 0, 3) + '</pre>');
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您没有达到第三个,因为在您的第二个subset1<-function(id){df2<-df[df$ID==id,]} 语句中,if将在定义时评估为true,并且是一个空列表。你可以自己检查一下:

wos.word.wordForms[0].synonyms

但紧接着它,您尝试访问不存在的if (wos.word.wordForms[0].synonyms) { alert('Yes!'); } // will alert 'Yes!' ,因此wos.word.wordForms[0].synonyms[0]。您的TypeError: wos.word.wordForms[0].synonyms[0] is undefined

也是如此

为了解决这个问题,请检查列表的长度:

wos.word.wordForms[0].sampleSentences