Whether to use [array] .filter or _.filter

my project includes underscores as a dependency. Inside, I need to perform many complex operations with arrays, which basically include displaying or filtering or decreasing the array. We have a built-in map, filter, reduction methods on Array.prototype. But the same methods are available in underscorejs.

Personally, it makes more sense for me to use my own methods, since it seems more natural instead of a wrapped object such as _(array).filter(function(){}) or, possibly, _.filter(array, function(){}) .

Please offer.

+5
source share
2 answers

If you need a rather complicated operation, go to underline with _.chain()

So you can call the call like this:

  _.chain(array).filter(function(){}).pluck('name').unique(); 

This example will retrieve all the unique name of the matched data in the filter.

Unlike the native function, this library was designed to be easier to use and provide good performance without any browser compatibility issues.

+2
source

This is indeed an opinion based question. Lodash will give you better browser support and possibly better performance, while native functions may be more clear on what they do. Native functions also handle some cases of edges with sparse arrays, etc., which may or may not be relevant to you.

No matter what your boat floats.

Personally, I would go for consistency. If you already use underscore or lodash for your functions that are not executed initially (e.g. _.uniq or _.pick ), I will just use _.filter and something else.

+1
source

Source: https://habr.com/ru/post/1247340/


All Articles