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
3 Answers
Reset to default 7It 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
版权声明:本文标题:javascript - check if value greater than a number exists in 2D array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744586746a2614241.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论