admin管理员组

文章数量:1389779

I have a 2d array in the format

emi_309 | weak   | 16
emi_310 | strong | 9
emi_319 | medium | 8
emi_315 | weak   | 5

I want to check if a value greater that a certain number (e.g > 5) exists in the 3rd column using a simple function like the following:

  ($.inArray( conditions greater than 5 here, $.map(myArray, function(v) { return v[2]; })) > -1)

How do I include the condition (>5) in the above function?

Thanks a lot for your help

I have a 2d array in the format

emi_309 | weak   | 16
emi_310 | strong | 9
emi_319 | medium | 8
emi_315 | weak   | 5

I want to check if a value greater that a certain number (e.g > 5) exists in the 3rd column using a simple function like the following:

  ($.inArray( conditions greater than 5 here, $.map(myArray, function(v) { return v[2]; })) > -1)

How do I include the condition (>5) in the above function?

Thanks a lot for your help

Share Improve this question asked Feb 12, 2013 at 12:00 KimKim 2,1966 gold badges34 silver badges46 bronze badges 3
  • 1 JavaScript has jagged arrays not multidimensional arrays – Rune FS Commented Feb 12, 2013 at 12:02
  • What is your actual data structure? – Scott Sauyet Commented Feb 12, 2013 at 12:02
  • 1 jQuery's inArray only checks if a particular value is included. It does not check an arbitrary condition. – Scott Sauyet Commented Feb 12, 2013 at 12:05
Add a ment  | 

3 Answers 3

Reset to default 7

It is better to use $.grep in this case:

if ($.grep(myArray, function(v) { return v[2] > 5; }).length) {
    // values > 5 exist
}

The $.inArray only tests against a specific value (checks equality) so it will not help you in this case.

you could change the inArray to be able to handle a predicate as it's first argument. Changing "base libraries" like this should be done carefully and you might want to create a different function. However in this specific case I feel that the ability to pass a function resembles that of other jQuery functions

var org_inArray = $.inArray
$.inArray = function(value, array, fromIndex){
  if($.isFunction(value)){

      var start = fromIndex || 0,
          matches = $.grep(myArray.slice(start), function(e,i) { 
         if(value(e,i)) return i;
      }
      return matches ? matches[0] : -1; 
  } else {
     arguments.length > 2 ? 
             org_inArray.call($,value,array,fromIndex) :
             org_inArray.call($,value,array)
  }
}

you would then be able to use it like

if($.inArray( function(e) { return e[2] > 5;}, myArray) !== -1){
}

Something like this is pretty simple:

var firstMatch = function(arr, cond) {
    for (var i = 0, len = arr.length; i < len; i++) {
        if (cond(arr[i])) {return i;}
    } 
    return -1;
};

if (firstMatch(myArray, function(v) { return v[2] > 5; }) > -1) { ... }

If you really want this in the jQuery namespace, just attach it to jQuery:

jQuery.firstMatch = function(Arr, cond) { ...

and call it like

if ($.firstMatch(myArray, function(v) { return v[2] > 5; }) > -1) { ... }

本文标签: javascriptcheck if value greater than a number exists in 2D arrayStack Overflow