seannyc7
seannyc7

Reputation: 11

Javascript callback function with boolean values

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;
        }
   });
};

How to write a callback function that would output each boolean value of the callback?

// example, even of an array, [1,2,3,4,5] -> false, true, false, true, false

Upvotes: 0

Views: 2317

Answers (4)

soddoff Baldrick
soddoff Baldrick

Reputation: 186

I am not sure where you want to use this: in a browser? Old Scripting environment? Node? but it seems to me you are trying to re-invent the wheel here.

The Javascript builtin array functions include the Find, Foreach and Map. I am not sure how you want the output either, so I'll just log it.

In this case you can use Foreach In long hand..

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

which will output

false
true
false
true
false

If you are familiar with arrow functions this is even simpler

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

If you want to do this asynchronously, then you can wrap in a promise in most modern environments

    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));

Upvotes: 0

scraaappy
scraaappy

Reputation: 2886

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);

Upvotes: 0

Paul
Paul

Reputation: 2076

If you want to save whether the callback returned true or false for each element, you need to store the single return values in a new array (as Ele already suggested);

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 0

Ele
Ele

Reputation: 33726

  • Within the function filter is missing the newArray with the filtered items.
  • This is an alternative:
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; }

Upvotes: 1

Related Questions