var array = [1,2,function() { }, 3, function() { }];
var cb = function() { console.log("foo"); }
var found = false;
console.log(_.map(array, function(val) {
if (_.isFunction(val) && !found) {
return found = true, _.compose(cb, val);
}
return val;
}));
This loop goes through the array and turns the first function that it finds into a folded function.
I hate this variable / counter found = false. How can I get rid of it?
Like an algorithm.
let found be 0
map value in array
if value satisfies condition and found is 0
let found be 1
return mutate(value)
else
return value
Update
Using a for loop
for (var i = 0; i < array.length; i++) {
if (_.isFunction(array[i])) {
array[i] = _.compose(cb, array[i]);
break;
}
}
_.map, _, _.isFunction,_.compose
source
share