jQuery.grep:如何根据数组进行过滤?

时间:2017-06-20 20:02:26

标签: jquery

我正在使用jQUery.grep来编译一个JSON对象。在下面的示例中,我根据一个工作得很好的唯一ID过滤对象: 请参阅:https://jsfiddle.net/zzwcm9cs/18/

var arrChildOptions2 = [
        {id:1, name:'john'}, 
        {id:2, name:'mike'},
        {id:3, name:'ben'},
        {id:4, name:'brian'},
    ];

var result = jQuery.grep(arrChildOptions2, function( n, i ) {
  return ( n.id === 2);
});

$( "#result" ).text( JSON.stringify(result) );

现在我想根据数组进行过滤,但我真的不知道怎么做。下面的想法(当然不起作用)。预期结果id具有仅包含“John”和“Ben”的JSON对象:

var arrChildOptions2 = [
        {id:1, name:'john'}, 
        {id:2, name:'mike'},
        {id:3, name:'ben'},
        {id:4, name:'brian'},
    ];

var myArray = [1,3];

var result = jQuery.grep(arrChildOptions2, function( n, i ) {
  return ( n.id IN myArray);  <<<-- How to do it ?
});

$( "#result" ).text( JSON.stringify(result) );

1 个答案:

答案 0 :(得分:0)

我找到了答案:https://jsfiddle.net/6ma42602/

Sub ArrayToRows()
'Declare your array or arrays


'Array1 would correspond to your "Column A" array, I'm not sure of the names you want, _
'just change the variables to suit your needs

For i = LBound(Array) to UBound(Array)
    Cells(1,1).Value = Array1(i) & " " & Array2(i) & " " & Array3(i)
Next i

End Sub