Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 608
trogwar Avatar asked Apr 29 '13 02:04

trogwar


People also ask

What is some () in JavaScript?

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

What is the jQuery equivalent for on () of JavaScript?

In jQuery, you can listen to events for dynamically added elements using the on() function. The equivalent in JavaScript is addEventListener() function.

What is every () in JavaScript?

Definition and Usage. The every() method executes a function for each array element. The every() method returns true if the function returns true for all elements. The every() method returns false if the function returns false for one element.


2 Answers

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) { … })
like image 134
Alex D Avatar answered Sep 22 '22 05:09

Alex D


Array.prototype.some returns true or false, so you can do:

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

You don't say where the error comes from, is it from the call to isNumber?

Edit

Ah, so your issue is with some.

If you want a jQuery some method, then it should at least mimic the built–in ECMAScript some, which takes two arguments: a callback function and an optional this argument.

The callback function should take three arguments: the value, the index (optional) and an optional value to use as the this argument. It should access the numeric members in ascending order and only visit members that actually exist.

So it should be something like (noting that 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;
}

So if you want now you can do:

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

or you can do either of the following to coerce true to 1 and false to 0:

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

or

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

The above is only lightly tested, the optional thisArg parameter might be redundant.

like image 24
RobG Avatar answered Sep 18 '22 05:09

RobG