10

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

user1486133
  • 1,309
  • 3
  • 19
  • 37
  • 2
    `some` returns a boolean value and if `true`, it ends the iteration. – Nina Scholz Feb 06 '17 at 15:22
  • 2
    `array.some` is doing as designed. [docs](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some) `array.find()` [docs](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find) may be more what you want – ste2425 Feb 06 '17 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](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some) 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` – Matt Burland Feb 06 '17 at 15:26

3 Answers3

11

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

Bartek Fryzowicz
  • 6,464
  • 18
  • 27
  • 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. – ibrahim mahrir Feb 06 '17 at 23:03
  • Wish there were something like `find()` but to be able to return something custom instead of just the element. – Drazen Bjelovuk Feb 25 '20 at 22:44
1

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.

bcr
  • 3,791
  • 18
  • 28
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

M B
  • 2,326
  • 24
  • 33