简体   繁体   English

函数内的 Underscore.js 函数

[英]Underscore.js function within a function

  _.filter = function(collection, test) {
    var result = [];
    _.each(collection, function(value) {
      if(test(value)) {
        result.push(value);
      }
    })
    return result;
  };
  _.reject = function(collection, test) {
    var result = [];
    return _.filter(collection, function(value) {
      return !test(value);
    })
  };

I'm a bit puzzled by how this works.我对它是如何工作的感到有点困惑。 I have two underscore.js functions defined here in the same scope.我在同一范围内定义了两个 underscore.js 函数。 If I pass a test array of random nums how does _.filter in _.reject work?如果我通过随机数的测试数组,_.reject 中的 _.filter 是如何工作的?

var isEven = function(num) { return num % 2 === 0; };
var odds = _.reject([1, 2, 3, 4, 5, 6], isEven);
expect(odds).to.eql([1, 3, 5]);

For example I test my functions and I get the assert is true but I don't understand how this works例如,我测试了我的函数,我得到断言为真,但我不明白这是如何工作的

Reject is just reusing some logic that filter accomplishes.拒绝只是重用过滤器完成的一些逻辑。 It could easily have been written this way:它可以很容易地写成这样:

_.reject = function(collection, test) {
  var result = [];
  _.each(collection, function(value) {
    if(!test(value)) {
      result.push(value);
    }
  })
  return result;
};

You will notice that the only thing difference between filter and reject is whether we want to keep items when the test is true, or when the test is false.您会注意到 filter 和 reject 之间的唯一区别是我们是要在测试为真时还是在测试为假时保留项目。

// Creates the function reject
_.reject = function(collection, test) {
  var result = [];

  // Calls filter but hands in a custom callback.
  // Filter iterates each item in the list, and keeps it
  // if test(item) returns true (test is our custom flipResult).
  // Remember that reject does the opposite, it keeps it
  // if test(item) returns false (aka !true).
  return _.filter(collection, function flipResult(value) {

    // The user gave us a test method. We want to keep items
    // that return false when passed to test.
    // If we call _.filter(collection, test) we will get all
    // items that return true when passed to test, but thats not what we want
    // So instead of handing test in directly, we hand in our custom callback
    // The custom callback calls test and flips the result.
    // This means filter will keep all items that return false
    // because our custom callback is flipping the result !test
    return !test(value);
  })
};

For example, if your array of numbers are:例如,如果您的数字数组是:

const nums = [1, 2, 3, 4, 5, 6]

and you have a function isEven which takes a number and returns true if it is even and false if it is not even (ie odd):并且你有一个函数isEven它接受一个数字,如果它是偶数则返回true如果它不是偶数(即奇数)则返回false

function isEven(num) {
  return num % 2 === 0;
}

Then running _.filter(arr, isEven) will perform the following logic:然后运行_.filter(arr, isEven)将执行以下逻辑:

  • arr takes the name of collection and isEven takes the name of test arrcollection名称, isEventest名称
  • Declare an array called result .声明一个名为result的数组。
  • Loop through every number in even循环遍历每个数字偶数

    • For each number ( value ), check if calling test(value) gives a result of true .对于每个数字 ( value ),检查调用test(value)给出true的结果。
    • If test(value) is true, then add the number ( value ) to the end of the result array.如果test(value)为真,则将数字 ( value ) 添加到result数组的末尾。
    • Go to next number in the array collection转到数组collection中的下一个数字
  • return the result array.返回result数组。

So, filter() executes the isEven function for each number in your array, and if it returns true , it is added to a new array (ie: it is kept).因此, filter()为数组中的每个数字执行isEven函数,如果返回true ,则将其添加到新数组中(即:保留)。

_.reject() does the opposite to _.filter() - that is, it keeps all elements the callback function returns false for. _.reject()作用与_.filter()相反——也就是说,它保留回调函数为其返回false所有元素。 As you know _.filter() keeps values it returns true for, you can use _.filter() by negating the value of the boolean returned by test(value) .如您所知_.filter()保留返回true值,您可以通过否定test(value)返回的布尔值来使用_.filter() test(value) This way, when test(value) returns false , you negate it to be true , making the _.filter() method keep that element.这样,当test(value)返回false ,您将其否定为true ,使_.filter()方法保留该元素。 If test(value) returns true , then you will negate that to be false , making the filter method discard of that value .如果test(value)返回true ,那么您将否定它为false ,使 filter 方法丢弃该value

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

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