Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lodash map with function: how to pass to function another parameter?

I'm using lodash map in this way

const newCollection = _.map(
    responseJson.OldCollection, 
    AddDetailsToSingleItems
);

having

function AddDetailsToSingleItems (item ) {
 ... 
}

I ask you, please, what's the right syntax to pass a parameter to this function.

I'd like to have

function AddDetailsToSingleItems (item, myNewParam ) {
 ... 
}

(note the myNewParam).

I don't kow how to tell to loadsh map to pass every single item to my function and to pass one more param when calling AddDetailsToSingleItems

like image 564
realtebo Avatar asked Nov 16 '25 12:11

realtebo


1 Answers

You haven't explained where myNewParam is coming from, but I assume you have that data somewhere.

Lodash map (just like normal JavaScript map) accepts a function as its second argument to be run on all elements of the supplied list. You can just pass an anonymous function:

_.map(
  responseJson.OldCollection, 
  item => AddDetailsToSingleItems(item, myNewParam)
);
like image 142
Hunter McMillen Avatar answered Nov 18 '25 19:11

Hunter McMillen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!