具有布尔值的Javascript回调函数

时间:2018-03-19 21:06:29

标签: javascript callback

var each = function(collection, callback){
    if(Array.isArray(collection)){
        for(var i=0;i<collection.length;i++){
            callback(collection[i]);
        }
    }else{
        for(var key in collection){
            callback(collection[key]);
        }
    }
};


var filter = function(collection, callback){
    each(collection, function(item){
        if(callback(item)){
            return item;
        }
   });
};

如何编写一个回调函数来输出回调的每个布尔值?

//示例,即使是数组,[1,2,3,4,5] - &gt; false,true,false,true,false

4 个答案:

答案 0 :(得分:1)

  • filter函数中缺少带有过滤项的newArray
  • 这是另一种选择:
var result = filter([1,2,3,4,5], function(n) {
    return n % 2 === 0;
});

var each = function(collection, callback) {
  if (Array.isArray(collection)) {
    for (var i = 0; i < collection.length; i++) {
      callback(collection[i]);
    }
  } else {
    for (var key in collection) {
      callback(collection[key]);
    }
  }
};

var filter = function(collection, callback) {
  var newArray = [];
  each(collection, function(item) {
    if (callback(item)) newArray.push(item);
  });

  return newArray;
};

var result = filter([1, 2, 3, 4, 5], function(n) {
  return n % 2 === 0;
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

如果要保存回调是否为每个元素返回truefalse,则需要将单个返回值存储在新数组中(如Ele建议的那样);

&#13;
&#13;
var each = function(collection, callback) {
  if (Array.isArray(collection)) {
    for (var i = 0; i < collection.length; i++) {
      callback(collection[i]);
    }
  } else {
    for (var key in collection) {
      callback(collection[key]);
    }
  }
};

var filter = function(collection, callback) {
  var newArray = [];
  each(collection, function(item) {
    newArray.push(callback(item));
  });
  
  return newArray;
};

var source = [1,2,3,4,5];
var result = filter(source, function(n) {
  return n % 2 === 0;
});

console.log("source array", source);
console.log("result array", result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

const source = [1,2,3,4,5];
const isEven = n => n%2 === 0;

const result = source.map(el=>isEven(el));
console.log("source array", source);
console.log("result array", result);

答案 3 :(得分:0)

我不确定你想在哪里使用它:在浏览器中?旧脚本环境?节点?但在我看来,你正试图在这里重新发明轮子。

Javascript内置数组函数包括Find,Foreach和Map。我不确定你想要的输出,所以我只是记录它。

在这种情况下,您可以长期使用Foreach ..

&#13;
&#13;
    var nums=[1,2,3,4,5];
    nums.forEach(function(element) {
        console.log(element%2==0)
    });
&#13;
&#13;
&#13;

将输出

false
true
false
true
false

如果您熟悉箭头功能,这甚至更简单

&#13;
&#13;
    var nums=[1,2,3,4,5];
    nums.forEach(element => {console.log(element%2==0)});
&#13;
&#13;
&#13;

如果您想异步执行此操作,那么您可以在大多数现代环境中包含承诺

&#13;
&#13;
    var nums=[1,2,3,4,5];
    var x= new Promise( (resolve) => {
            var result=[];
            nums.forEach(function(element) {
                result.push(element%2==0);
            });
            resolve(result);
            })
        .then(evens=>console.log(evens));
&#13;
&#13;
&#13;