I have written this function, that basically emulates the Underscore.js function of the same name. All is working correctly, except I am struggling to understand how to bind to the context if one is passed. I'm sure I should be using function.prototype.bind(), but I'm unsure on exactly how to implement it.
// _.each(collection, iteratee, [context])
// Iterates over a collection of elements (i.e. array or object),
// yielding each in turn to an iteratee function, that is called with three arguments:
// (element, index|key, collection), and bound to the context if one is passed.
// Returns the collection for chaining.
_.each = function (collection, iteratee, context) {
if(Array.isArray(collection)){
for(let i = 0; i < collection.length; i++){
iteratee(collection[i], i, collection);
};
};
if(typeof collection === 'object' && !Array.isArray(collection)){
Object.entries(collection).map(([key, value]) => {
iteratee(value, key, collection);
});
};
return collection;
};
You can use .call
to invoke the iteratee
with a specific context:
iteratee.call(context, value, key, collection);
or create a permanently-bound version of the iteratee
function and use it as its already used.
iteratee = iteratee.bind(context);
// calls to iteratee(...) will always have `this` as `context`