Created
October 24, 2020 15:18
-
-
Save luojiyin1987/452c8564ebfd4b52ca5b223eb8e98fa9 to your computer and use it in GitHub Desktop.
尝试使用函数式编程 解耦复杂度
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[][]} restaurants | |
| * @param {number} veganFriendly | |
| * @param {number} maxPrice | |
| * @param {number} maxDistance | |
| * @return {number[]} | |
| */ | |
| var filterRestaurants = function(restaurants, veganFriendly, maxPrice, maxDistance) { | |
| function isVeganFriendly(rest) { | |
| if (veganFriendly === 1) | |
| { | |
| return rest[2] === 1 | |
| } else { | |
| return true; | |
| } | |
| } | |
| function isLessMaxPrice(rest) { | |
| return rest[3] <= maxPrice; | |
| } | |
| function isLessMaxDistance(rest) { | |
| return rest[4] <= maxDistance; | |
| } | |
| const restaurants1 = restaurants.filter(rest=>isVeganFriendly(rest)); | |
| const restaurants2 = restaurants1.filter(rest=>isLessMaxPrice(rest)); | |
| const restaurant3 = restaurants2.filter(rest=>isLessMaxDistance(rest)); | |
| const reuslt = restaurant3.sort((a,b) =>a[1] === b[1] ?b[0]-a[0]:b[1]-a[1]) | |
| return reuslt.map(item =>item[0]) | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment