Less code than importing and calling the lodash method union. Now we can compare our arrays just by reference like updatedUsers = users which is super quick! React.js uses this approach to speed up the reconciliation process. Here instead of changing the single item in our array, we create a new one with only one element different. The filter() method creates a new array with all elements that pass the test implemented by the provided function. Well, this is the only one not about map/ reduce/ filter, but it’s so compact that it was hard not to put it in the list. Remove duplicates from an array of numbers/strings If you have a good example of using a map or reduce method - post it in the comments section. It surprised me how simplified the codebase became. Recently I rewrote a few modules using these great functions. But, what I’ve seen a lot is 7–8 line for-loop statements for solving a regular task where Array.reduce could do it in one line. Moreover, I’ve never seen it in a real codebase. When you read about Array.reduce and how cool it is, the first and sometimes the only example you find is the sum of numbers. By Alex Permyakov How to simplify your codebase with map(), reduce(), and filter() in JavaScript Photo by Anders Jildén on Unsplash
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |