How to refactor a for loop with lodash _.filter vs _.forEach?

I;m trying to implement lodash to refactor this for loop, what would BE THE best way to go for it _.filter or a _.forEach ?

for(var i = 0; i < origPam.length; i ++){
                if(origPam[i].editor.mapped !==newPam[i].editor.mapped){
                    $scope.$apply();
                }
            }

Seem you have 2 input array. I will use _.zip to pair them first.

const pams = _.zip(origPam, newPam)

Then filter by condition.

const pamsToProcess = 
  _.filter(pems, ([origPem, newPem]) => origPem.editor.mapped !== newPem.editor.mapped)

Finally _.forEach to do stuff.

_.forEach(pamsToProcess, ([origPem, newPem]) => {/* do stuff */})

Combine above

_(_.zip(origPam, newPam))
  .filter(([origPem, newPem]) => origPem.editor.mapped !== newPem.editor.mapped)
  .forEach(([origPem, newPem]) => {/* do stuff */})

Because you cannot use break in _.forEach, you can use _.takeWhile to filter out things first.

Pure JavaScript Functions as a Replacement for , Run results for: _.filter vs array filter - MeasureThat.net Just because forEach is native does not mean that it is faster than a simple loop built  Lodash forEach; jQuery each() Iterating Over an Associative Array; Summary; As the language has matured so have our options to loop over arrays and objects. JavaScript objects are also arrays, which makes for a clean solution to index values by a key or name. The Basic For Loop. JavaScript for loops iterate over each item in an array.

origPam.forEach(function(val, i) {
  if (val.editor.mapped !== newPam[i].editor.mapped)
    // do stuff
});

To be honest you should probably optimize this (there's no point calling $apply more than once)

Underscore.js, each _.each(list, iteratee, [context]) Alias: forEach Note: Collection functions work on arrays, objects, and array-like objects such as It's also good to note that an each loop cannot be broken out of — to break, use _.find instead. var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); => [2, 4, 6]. When to use a for loop over forEach. forEach comes with a little caveat. It only works on arrays. So if you want to actually count, forEach probably isn’t your friend.

You shouldn't use filter() unless you plan on using the results. For example, you could use filter(), then use forEach() to iterate over those results, calling the method, or doing whatever else.

Or you could keep it simple and filter as the first step in the forEach() callback. Since you're not building results to use later, this might be the simplest approach.

Mastering Javascript Arrays: Beyond “for” and “forEach” loops, Mastering Javascript Arrays: Beyond “for” and “forEach” loops find() and filter() are designed to encapsulate the heavy looping work to retrieve (and the other ones that you eventually pass by and make some refactoring). The main difference between forEach and filter is that forEach just loop over the array and executes the callback but filter executes the callback and check its return value. If the value is true element remains in the resulting array but if the return value is false the element will be removed for the resulting array.

Replace your loops by Array methods - map() - Daily JS Tips, The `map()` method works like `forEach()`: this method iterates by an array, and receives two parameters: a callback function, and a second parameter, that  You shouldn't use filter() unless you plan on using the results. For example, you could use filter(), then use forEach() to iterate over those results, calling the method, or doing whatever else. Or you could keep it simple and filter as the first step in the forEach() callback. Since you're not building results to use later, this might be the simplest approach.

Don't use Array.forEach, use for() instead (Example), A protip by afshinm about performance, loop, and javascript. ForEach is about 95% slower than for() in for each for Arrays in JavaScript. you will find most of your optimizations will not be refactoring map, filter, or reduce). If the fact that it’s easier, isn’t enough for you… there is also a technical reason to use .forEach() in favor of for loops. It has to do with the scoping of the code. When using the .forEach() you pass an individual function with it’s own scope. In a for loop you’re polluting whatever scope you place the loop in.

How to Use Map, Filter, & Reduce in JavaScript, JavaScript also offers a forEach loop. Just like map and filter , reduce is defined on Array.prototype and so available on any array, and you  Using Filter. Array.Filter returns a new array of elements which pass the given test functions. It loops over all the elements of the source array, calling test function for each item, if test function returns true, the element will be added to the new array. With Filter function, we can just supply the core test logic of the function,

Comments
  • Why do you want to implement your own digest? Anyway, as there are no breaks, _.forEach should be fine, so you won't create any new collection along the way.
  • I prefer the filter, map, reduce methods, they're simpler to code and tell you what is happening, unlike a vague for or forEach. Must you use lodash? Why not plain old JavaScript filter?
  • What's wrong with plain-old forEach that's been in JavaScript since it crawled out of the primordial tar pits?
  • @tadman sadly, primordial ooze only recedes at the dawn of IE9. That said, you can polyfill a happy foundation right over top of that tarpit, and live mappily ever after.