Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luojiyin1987/452c8564ebfd4b52ca5b223eb8e98fa9 to your computer and use it in GitHub Desktop.
Save luojiyin1987/452c8564ebfd4b52ca5b223eb8e98fa9 to your computer and use it in GitHub Desktop.
尝试使用函数式编程 解耦复杂度
/**
* @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