0

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}

2
  • 2
    possibly change of the data structure? Commented May 18, 2016 at 9:56
  • smells like a recursive approach is required :-) Commented May 18, 2016 at 9:57

2 Answers 2

1

Use &&(AND) logical operator between conditions, if first condition fails second condition will not check and so on

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>');

Sign up to request clarification or add additional context in comments.

5 Comments

I tried this and updated the answer. When I debug every line the code gets as far as the check for synonyms and then no further in the chrome developer tools debugger. So it seems that the if() is causing it to exit. Do you have any ideas what might cause this?
Can you explain what this means. How could I check for this so the code doesn't stop or exit ? I updated the question to show the data so many that helps. Thanks
thanks. Can you add in the three checks that I have so we could see if it gets to end and sets the Ids. I would do this but I am not sure how to edit a code snippet that you created. Thanks
@SamanthaJ : just put an alert or console.log statement inside the if
I tried adding the three condition checks in my own snippet but it doesn't run to the end. Do you think your answer is maybe not accounting for all possibilities?
0

You don't reach the third if, because in your second if statement, the wos.word.wordForms[0].synonyms will evaluate to true as it is defined and is an empty list. You can check it yourself:

if (wos.word.wordForms[0].synonyms) { alert('Yes!'); } // will alert 'Yes!'

But right next to it, you try to access wos.word.wordForms[0].synonyms[0], which does not exist, hence the TypeError: wos.word.wordForms[0].synonyms[0] is undefined. The same situation is with your wos.word.wordForms[0].sampleSentences

In order to fix this, check the length of the list as well:

if (wos.word.wordForms 
  && wos.word.wordForms[0].synonyms 
  && wos.word.wordForms[0].synonyms.length  // <-- check for element existence
  && wos.word.wordForms[0].synonyms[0].synonymId) {
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.