最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Whether to use [array].filter or _.filter - Stack Overflow

programmeradmin1浏览0评论

my project includes underscorejs as a dependency. Internally I need to do a lot of plex array operations which basically includes me mapping over or filtering or reducing an array. We have native map, filter, reduce methods on Array.prototype. But the same methods are also available in underscorejs.

Personally, it makes more sense for me to use the native methods as it feels more natural in place of a wrapped object like _(array).filter(function(){}) or maybe _.filter(array, function(){}).

Please suggest.

my project includes underscorejs as a dependency. Internally I need to do a lot of plex array operations which basically includes me mapping over or filtering or reducing an array. We have native map, filter, reduce methods on Array.prototype. But the same methods are also available in underscorejs.

Personally, it makes more sense for me to use the native methods as it feels more natural in place of a wrapped object like _(array).filter(function(){}) or maybe _.filter(array, function(){}).

Please suggest.

Share Improve this question edited Apr 18, 2016 at 7:48 Knu 15.2k6 gold badges59 silver badges92 bronze badges asked Apr 18, 2016 at 7:38 Rohan BagchiRohan Bagchi 7111 gold badge10 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

This is really an opinion based question. Lodash will give you better browser support and possibly better performance, while the native functions might be arguably more clear on what they are doing. The native functions also handle some edge cases with sparse arrays and such, which may or may not be relevant to you.

Whatever floats your boat.

Personally I'd go for consistency. If you are already using underscore or lodash for their functions that aren't natively implemented (like _.uniq or _.pick) I'd just keep using _.filter and whatnot too.

If you need quite some plex operation go for underscore with the _.chain()

So you can chain call like this :

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

This sample will extract all unique name of the matched data in the filter.

Unlike native function, this library has been developped to be more easy to use and provide a good performance without having any problems with browser patibility.

发布评论

评论列表(0)

  1. 暂无评论