0

I'm only sharing a small bit of code because there is so much going on, and I hope this is enough to answer my question.

I have some existing JS where a value is determined with an OR statement and I think I need to convert that to an IF statement. The final output is currently giving me both values if they both exist, and I only want "question" where both "question" and "name" values exist.

var question = new fq.Question(questionData.answerId, topicId,
        questionData['question'] || questionData['name'],
        questionData['text']);

Instead of using the OR operator (answerData['question'] || answerData['name']), I'd like to do something similar to the following:

if (questionData['question'] is undefined) {
  use questionData['question'];
} else { 
  use instead questionData['name'] 
}

But, I don't know how I might accomplish such a statement within the () in the existing code pasted above. The name variable/value is always present, so there's no risk in defaulting to that. Question is only defined some of the time. And I don't ever want both appearing.

This is probably outside of the scope of my query here, but to fill in a little more detail, this code eventually outputs JSON files for topics and questions. Topics only have names values, and questions have both names and questions, but I only want the questions json to include questions values, not names. I'm pretty sure this is the key part in all of the JS to determin

david
  • 123
  • 2
  • 13
  • 2
    "*The final output is currently giving me both values if they both exist*" - no, `||` would never do that. There must be something else that is wrong. Show us your actual code that produces the problem, please. – Bergi Dec 01 '16 at 01:12
  • You could use a helper variable (to which you assign conditionally), a helper function (from which you return conditionally) or just a ternary operator (as an inline expression), but neither of those will behave different from the `||` operator. – Bergi Dec 01 '16 at 01:14
  • 1
    Did you mean "Use `questionData['question']` when it is **not** `undefined`"? – Bergi Dec 01 '16 at 01:14

1 Answers1

2

Create a function and get value from there.

Need to remember scope of function:

Example Snippet:

var that = this;
var question = new fq.Question(questionData.answerId, topicId,
  that.getValue(),
  questionData['text']);

function getValue() {
  if (questionData['question']) { //null and undefined both are false
    return questionData['question']
  } else {
    return questionData['name']
  }
}
Ajay Narain Mathur
  • 4,901
  • 2
  • 15
  • 29
  • 2
    Why bother with the if statement? If we're only considering truthiness then the or operator is sufficient. If the OP is "getting both values" then something else is wrong. I do approve of refactoring in case the logic changes, and it's way easier to test. – Dave Newton Dec 01 '16 at 01:19
  • Thank you. This is what I had vaguely in my head, that I'd have to create a function outside of the `var question` equation, just wasn't sure exactly how to go about it. – david Dec 01 '16 at 01:21
  • Dave Newton is right. There is somewhere else I need to look because I'm still outputting both question and name in my JSON, but I didn't offer any more code here and this answered the literal question I asked, so I've marked it as the accepted answer. – david Dec 01 '16 at 01:23