admin 管理员组文章数量: 1086019
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 badges2 Answers
Reset to default 3This 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.
本文标签: javascriptWhether to use arrayfilter or filterStack Overflow
版权声明:本文标题:javascript - Whether to use [array].filter or _.filter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743987107a2514053.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论