I am trying to check to see if wordDefinitionId is defined and not null. Here's what I have been trying to do but think even this code seems to give some problems. Is there an easy way for me to do this check and set the value of wos.wordDefinitionId to either the value (if it exists) or zero if it does not exist.
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
}
}
}
Note that if set then I want to get the wordDefinitionId that is in the first array position of wordDefinitions etc.
Update:
I tried the answer suggested:
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
However when I debug the code does not reach the final "if"
Here for reference are the objects:
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}
Same here but maybe easier to see:
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}