Luis Martins
Luis Martins

Reputation: 1481

Passing Lodash's iteration item from the forEach method to a function

Considering the following simple example:

_.each(activeListItems, function(element){ 
   toggleActiveItem(element);
});

How could I pass the item from each iteration to a "external" function, like this:

_.each(activeListItems, doSomething(item) );

Upvotes: 1

Views: 168

Answers (1)

Joe
Joe

Reputation: 2596

You're close.

_.each(activeListItems, doSomething);

function doSomething(element, index) {
  console.log(element, index);
}

Upvotes: 1

Related Questions