12

I'm trying to use an array.some function to iterate through some data and return my field if the if statement succeeds.

What I am finding is happening instead, is that I am getting a boolean return e.g true instead of the actual variable (which contains details to an element).

for (var index in allFields) {
      const invalidField = allFields[index].some(function (field){
        if (!validation.getIn([index,field.dataset.fieldKey,'isValid'])) {
          return field;
        }
      });
      if (invalidField) {
        return invalidField;
      }
    }

My code cycles through allFields, which contains lists of fields under indexes. It then compares each fieldKey with another set of data called validation.

field contains an element. I wish to return field but instead when I check invalidField I get true instead of the element

3
  • 2
    some returns a boolean value and if true, it ends the iteration. Commented Feb 6, 2017 at 15:22
  • 2
    array.some is doing as designed. docs array.find() docs may be more what you want Commented Feb 6, 2017 at 15:22
  • Your confusion here might be that you are returning field which is the element you want from your callback, but that's not what some returns. Per msn for the return value: true if the callback function returns a truthy value for any array element; otherwise, false. field is "truthy", so some returns true Commented Feb 6, 2017 at 15:26

3 Answers 3

14

Array.prototype.some() only checks if any element in the array passes test defined in callback function. You should use array find method which returns first element passig test

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

2 Comments

Just note that find return an item from the array if matched. Or undefined otherwise, so it's better to check if it returned undefined before attempting to use that returned value.
Wish there were something like find() but to be able to return something custom instead of just the element.
2

You're looking for Array.prototype.filter instead of Array.prototype.some.

Filter does what you're currently expecting some() to do. some() returns a boolean if 1 or more elements meet your criteria while filter() creates a new array of elements that met your criteria.

Comments

1

to get the element, you need to use array.filter()

for (var index in allFields) {
  const invalidField = allFields[index].filter(function (field){
    if (!validation.getIn([index,field.dataset.fieldKey,'isValid'])) {
      return field;
    }
  });
  if (invalidField.length > 0) { //check if returned anything
    return invalidField[0];
  }
}

If you want to return only the first, you can use array.find()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

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.