简体   繁体   English

将参数传递给jquery $ .grep

[英]passing parameter to jquery $.grep

If I have this pice of code which checks is something already exist inside array 如果我有这段代码可以检查数组内部是否已经存在

var array = [ 1, 5, 12, 31, 7, 69 ];

var array = $.grep(array, function(n, i) {
  return (n == 112);
});

alert(array.length);

my question is simple one: How can I pass variable to this grep function and to use it instead of hardcoded 112 value inside expression? 我的问题很简单:如何将变量传递给此grep函数并使用它而不是表达式中的硬编码112值?

You can just pass a variable from the outside. 您可以只从外部传递变量。 JavaScript's lexical scope allows for variables from the outside scope to be passed into deeper functions. JavaScript的词法作用域允许将外部作用域中的变量传递给更深层的函数。

http://jsfiddle.net/ag1djcjm/ http://jsfiddle.net/ag1djcjm/

var array = [ 1, 5, 12, 31, 7, 69];
var code = 112;

// No need to declare variables twice
array = $.grep(array, function(n, i) {
  return (n == code);
});

alert(array.length);

Try like this 这样尝试

var array = [ 1, 5, 12, 31, 7, 69 ];

var val=112;
// remove var from here it'll re-declare same variable
array = $.grep(array, function(n, i) {
  return (n == val);
});

alert(array.length);

JSFIDDLE JSFIDDLE

You can do it by javascript's .filter() also 您也可以通过javascript的.filter()来完成此操作

Like this 像这样

var array = [ 1, 5, 12, 31, 7, 69 ];
var val=112;
array = array.filter(function(n) { return n == val; });
alert(array.length);

JSFIDDLE JSFIDDLE

Just define the value you need to filter before calling $.grep function. 只需在调用$ .grep函数之前定义需要过滤的值即可。 See example below 见下面的例子

var array = [1, 5, 12, 31, 7, 69],
    filterValue = 5;

var newArray = $.grep(array, function (n, i) {
    return n == filterValue;
});

Since you're redefining the array, I created a new variable newArray and assigned filtered values to that, but you can still assign it to array variable. 由于您正在重新定义数组,因此我创建了一个新变量newArray并为其分配了过滤后的值,但是您仍然可以将其分配给array变量。

Code: 码:

function filter ( data, val ) {
    return $.grep(data, function ( n, i ) {
        return (n == val);
    });
}

Test: 测试:

var array = [ 1, 5, 12, 31, 7, 69];
var test = filter(array, 112);
alert(test.length);

So, what you're basically trying to do, it determine if an array of numbers contains a certain number (variable). 因此,您基本上要尝试执行的操作是确定数字数组是否包含某个数字(变量)。

There's no need to over-complicate this with grep. 无需使用grep使其过于复杂。 Just use indexOf : 只需使用indexOf

 var array = [ 1, 5, 12, 31, 7, 69 ], search = 112, hasNumber = array.indexOf(search) !== -1; // Using this so that it's visible in the snippet. document.body.textContent = hasNumber; 

Could create a function outside of $.grep() , to be called for each item in array ; 可以在$.grep()之外创建一个函数,以对数组中的每个项目调用该函数; could also pass additional parameters to function . 也可以传递其他参数来起作用。 Also try defining different variable names for input array , resulting array 也尝试为输入数组定义不同的变量名,结果数组

var array = [ 1, 5, 12, 31, 7, 69 ], num = 112
, compare = function(n, i) { return n === num }

var res = $.grep(array, compare);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM