In my coding project, I am using a custom 'each()' function that iterates through a collection and performs a specific function on it.
_['each'] = function(collection, iterator) {
if (Array.isArray(collection) === false && typeof(collection) ===
'object') {
var values = Object.values(collection);
var keys = Object.keys(collection);
for (let i = 0; i < values.length; i++) {
iterator(values[i], keys[i], collection, i);
}
} else {
for (let i = 0; i < collection.length; i++) {
iterator(collection[i], i, collection);
}
}
}
Now, I am trying to create a new 'map()' function that will take the results of the operations performed by 'each()' and return a fresh array. However, I am facing difficulty in finding a way to access the values extracted during iterations when implementing 'map()'. Should I modify 'each()' to include an additional callback parameter for adding values to a collection?