📜  使用递归过滤函数 - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:41.827000             🧑  作者: Mango

代码示例1
function exclude(arr, fn, output) {
  output || (output = []);
  
  if(!arr.length) { 
    return output;
  }
  
  if (fn(arr[0])) {
    output.push(arr[0]);
  }

  return exclude(arr.slice(1), fn, output);
}

console.log(exclude([1,2,3,4,5,6,7,8,9], function(i) { return i % 2; }));
 Run code snippet