0

I have created a function which, when called with two arguments, checks the arguments against some supplementary data and if the return is false, returns the field.

I wish to return one of the elements if false and ignore/ return undefined if true.

  getFirstInvalidField() {
    const allFields = this.getPassengerDetailsFieldsRefs();
    allFields[0].find(function (field){
      if (!this.isValidField(field, 0)) {
        return field;
      }
    }, this);
  }

So when isValidField returns false, it should return field to the place where getFirstInvalidField() was called. This does not seem to be happening.

const firstInvalidField = this.getFirstInvalidField();

This should return field but instead returns undefined always. It seems the return field statement is not getting passed back and assigned to firstInvalidField

3
  • 1
    The docs says "The find() method returns a value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned." Commented Feb 8, 2017 at 15:05
  • @Teemu yes, and it should have been satisfied and returned? Commented Feb 8, 2017 at 15:07
  • 3
    Put a return before allFields[0].find(). You're returning the result into the function, but not then returning the result from the function. Commented Feb 8, 2017 at 15:07

2 Answers 2

3

You need to return in function getFirstInvalidField() the value returned by allFields[0].find() as such:

getFirstInvalidField() {
  const allFields = this.getPassengerDetailsFieldsRefs();
  return allFields[0].find(function (field){
    if (!this.isValidField(field, 0)) {
      return field;
    }
  }, this);
}
Sign up to request clarification or add additional context in comments.

Comments

-1

You need to return a true or false response from find() callback. Also return the result to function caller.

getFirstInvalidField() {
  const allFields = this.getPassengerDetailsFieldsRefs();
  return allFields[0].find(function (field){
    return !this.isValidField(field, 0);
  }, this);
}

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.