【问题标题】:JavaScript filter WITHOUT .filter function没有 .filter 函数的 JavaScript 过滤器
【发布时间】:2022-01-07 15:12:17
【问题描述】:

我正在尝试编写一个不使用.filter 函数来过滤数组的函数。这是到目前为止我编写的函数;

function filter(ray, fn) {
    //The easy way
    //let filterArray = ray.filter(fn);
    //return filterArray;

    let filterArray = [];
    for (let i = 0; i < ray.length; ++i) {
        if (fn(i) === true) {
            filterArray.push(i);
        } else {
            //do nothing            
        }
    }
    return filterArray;
}

用作fn的函数是;

function isOdd(x) {
    return x % 2 === 1;
}  
function alwaysTrue(x) {
    return true;
}  
function alwaysFalse(x) {
    return false;
}

该函数目前可与alwaysFalse 函数一起使用,但不能与其他两个一起使用。我哪里错了?

function filter(ray, fn) {
    //The easy way
    //let filterArray = ray.filter(fn);
    //return filterArray;

    let filterArray = [];
    for (let i = 0; i < ray.length; ++i) {
        if (fn(i) === true) {
            filterArray.push(i);
        } else {
            //do nothing            
        }
    }
    return filterArray;
}

function isOdd(x) {
    return x % 2 === 1;
}  
function alwaysTrue(x) {
    return true;
}  
function alwaysFalse(x) {
    return false;
}

console.log(filter([1,2,3,4], isOdd)); // [1,3]
console.log(filter([1,2,3,4], alwaysFalse)); // []

【问题讨论】:

    标签: javascript arrays loops if-statement filter


    【解决方案1】:

    你推送的是索引而不是元素

    filterArray.push(i);
    // should be
    filterArray.push(ray[i]);
    

    你也在索引而不是元素上调用函数

    if (fn(i) === true) {
    // should be
    if (fn(ray[i]) === true) {
    

    除此之外,您的代码还可以。

    【讨论】:

      【解决方案2】:

      您正在检查fn(i),其中i 是for 循环的索引。您应该检查fn(ray[i]),或给定索引处的数组值。推送也是如此——你应该推送ray[i],而不是i

      function filter(ray, fn) {
          //The easy way
          //let filterArray = ray.filter(fn);
          //return filterArray;
      
          let filterArray = [];
          for (let i = 0; i < ray.length; ++i) {
              if (fn(ray[i]) === true) {
                  filterArray.push(ray[i]);
              } else {
                  //do nothing            
              }
          }
          return filterArray;
      }
      
      function isOdd(x) {
          return x % 2 === 1;
      }  
      function alwaysTrue(x) {
          return true;
      }  
      function alwaysFalse(x) {
          return false;
      }
      
      const arr = [1, 2, 3, 4, 5, 6, 7];
      console.log(filter(arr, isOdd));

      【讨论】:

      • 天哪,现在完全有道理了。非常感谢您为我澄清这一点!
      【解决方案3】:

      您可以使用for...of 轻松循环遍历数组并将项目推送到新数组中。

      注意:如果满足条件,过滤器回调函数应该返回 true。

      function filter(arr, fn) {
        const filtered = [];
        for(const item of arr){
          if(fn(item)) filtered.push(item);
        }
        return filtered;
      }
      
      const isOdd = (x) =>  x % 2 === 1;
      
      const numbers = [1, 2, 3, 4, 5, 6, 7];
      console.log(filter(numbers, isOdd));

      【讨论】:

      • 这真是太棒了!我仍在加强我的 JS 技能,所以很高兴看到不同的方法来做同样的事情!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多