简体   繁体   English

使用“过滤器”重新创建underscore.js“拒绝”

[英]Recreating underscore.js 'reject' using 'filter'

I have a project which requires me to replicate underscore.js's 'reject' function using the 'filter' function. 我有一个项目,要求我使用“过滤器”功能复制underscore.js的“拒绝”功能。 I've written the following, though can't seem to get the tests to pass. 我写了以下内容,尽管似乎无法通过测试。 Any suggestions? 有什么建议么?

// Return all elements of an array that pass a truth test.
  _.filter = function(collection, test) {
    var passed = [];
    _.each(collection, function(item, index) {
      if (test(item) === true) {
        passed.push(item);
      }
    });
    return passed;
  };

  // Return all elements of an array that don't pass a truth test.
  _.reject = function(collection, test) {
    _.filter(collection, function(item) {
      return !test(item);
    });
  };

You forgot to return the result of filter . 您忘记了返回filter的结果。

_.reject = function(collection, test) {
  return _.filter(collection, function(item) {
    return !test(item);
  });
};

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

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