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

trogwar picture trogwar · Apr 29, 2013 · Viewed 15.3k times · Source

Was looking for "equivalent for some method in javascript" and "return just one value if is in array", but saw only the answers to the way in which to determine the type of variables or too many unnecessary.

I bypass all 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 it throws an error:

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


UPD: Error comes from the last line, yeah, 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 to work in most modern browsers?

UPD: Problem was solved. I'm a little confused in choosing the answer. The code of @RobG implementation of .some() is more understandable for beginners (and I am) so I switched my vote.

Answer

Alex D picture Alex D · Jul 18, 2013

For anyone else who comes to this thread, you can use some() on a jQuery object this way:

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

jQuery.makeArray() converts the jQuery object into an Array, so you can use some() on it.


As suggested by @alf-eaton, you could use:

$(…).toArray().some(function(node) { … })