【问题标题】:Async.js filtering an array not workingAsync.js 过滤数组不起作用
【发布时间】:2017-01-12 08:01:04
【问题描述】:

我正在尝试使用 async 的过滤方法,但没有得到我期望的结果

async.filter([1, 3, 5], function (item, done) {
        done(item > 1);
    }, function (results) {
        console.log(results);
    });
    async.filter([1, 2, 3, 4, 5, 6], function(item, callback) {
      if (item > 3) {
          callback(true);
      } else {
        callback(false);
      }

    },
    function (result) {
      console.log("result: " + result);
    });

输出是

是的

结果:是的

而不是 2 个过滤数组,我缺少什么?

【问题讨论】:

    标签: javascript node.js async.js


    【解决方案1】:

    我认为你应该使用一些不同的语法,就像这里指定的那样:async#filter

    回调中的结果应该是第二个参数(不是第一个):callback(null, true)

    例如:

    async.filter([1, 3, 5], function (item, done) {
        done(null, item > 1);
    }, function (err, results) {
        console.log(results);
    });
    async.filter([1, 2, 3, 4, 5, 6], function(item, callback) {
      if (item > 3) {
          callback(null, true);
      } else {
        callback(false);
      }
    
    },
    function (err, result) {
      console.log("result: " + result);
    });
    

    jsbin#example

    【讨论】:

      猜你喜欢
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2018-04-23
      • 2020-05-25
      • 2015-04-01
      • 2020-05-30
      • 1970-01-01
      相关资源
      最近更新 更多