Working equivalent of .some () method in javascript or jquery?

We are looking for "the equivalent for some method in javascript" and "returns only one value if it is in an array", but saw only answers on how to determine the type of variables or too many unnecessary ones.

I will go around all the inputs in html and I want something like this:

$('#goodsFilter')
    .find('input[type="number"]')
    .some(function(i,el){
        return (isNumber($(el).val())) ? 1 : 0;
});

But this causes an error:

"TypeError: 'undefined' is not a function" (e.g. Safari 6.0.4).


UPD: The error comes from the last line, yes, where });. IsNumber:

function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n); }

This should check for the presence of each input information, and if at least one of them is not empty, return 1, otherwise 0. How can I replace it with work in most modern browsers?

UPD: . . @RobG .some() ( ), .

+6
6

Array.prototype.some true false, :

.some(function(el){
        return !isNaN(el.value);
}

, , isNumber?

, .

- jQuery, ECMAScript, : .

: , () . , .

, - (, jQuery.fn === jQuery.prototype):

jQuery.fn.some = function(fn, thisArg) {
  var result;

  for (var i=0, iLen = this.length; i<iLen; i++) {

    if (this.hasOwnProperty(i)) {

      if (typeof thisArg == 'undefined') {
        result = fn(this[i], i, this);

      } else {
        result = fn.call(thisArg, this[i], i, this);
      }

      if (result) return true;
    }  
  }
  return false;
}

, , :

var result = $('#goodsFilter')
              .find('input[type="number"]')
              .some(function(el) {
                 return isNumber(el.value); 
              })? 1 : 0; 

, true 1 false 0:

var result = Number($('#goodsFilter')
              .find('input[type="number"]')
              .some(function(el) {
                 return isNumber(el.value); 
              }));

var result = +($('#goodsFilter')
              .find('input[type="number"]')
              .some(function(el) {
                 return isNumber(el.value); 
              }));

, thisArg .

+7

, , some() jQuery :

 $.makeArray($(...)).some(function(x) { ... })

jQuery.makeArray() jQuery , some() .

+7

.filter, .

$('#goodsFilter')
    .find('input[type="number"]')
    .filter(function(i,el){ return isNumber($(el).val())); })
    .length > 0
+5

., "" :

function eSome(arr, f) { var i = 0, n = arr.length;
  for (;i<n;i++) { if (!i in arr) { continue }
    if (f(i, arr[i])) { return true; }
  } return false;
}

var list = [0, 1, 2, 3, 4, 5];
var testFunction = function (i, e) { return e === 2; };
console.log(eSome(list, testFunction));
//returns true and the loop ran only for the necessary three times.

., .some jQuery, jQuery, ( ):

jQuery.fn.some = function (f) { var i = 0, n = this.length;
  for (;i<n;i++) { if (!i in this) { continue }
    if (f(i, this[i])) { return true; }
  }
  return false;
}

$('.a').some(function (i, el) { return ($(el).text() == 'weeee!'); });

., @RobG , Array.prototype.some . OP, ECMA if (f(this[i], i, this)) { return true; } .

., Array.prototype.some, .

+2

$(...).is(function) . jQuery API ():

Check the current consistent set of elements for a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments .

So, using the example in the question, we will have something like:

var result = $('#goodsFilter')
              .find('input[type="number"]')
              .is(function(idx, el) {
                  return isNumber(el.value); 
              })? 1 : 0; 
+2
source

Vanilla Javascript implementation (with arrow syntax)

function some(arr,callback){
  for(let i=0;i<arr.length;i++){
    if(callback(arr[i],i,arr)){
      return true;
    })
  }
  return false;
}

some use:

check if the array has an even number:

function hasEvenNum(arr){
  return arr.some(value=>{
    return value % 2 === 0;
  });
}
0
source

All Articles