From e750c8e19ccb5b5efeae211d93bc3a72685c201f Mon Sep 17 00:00:00 2001 From: liuliangsir <2269305724@qq.com> Date: Thu, 25 Jan 2018 18:33:16 +0800 Subject: [PATCH] Fix some issues --- snippets/reduceWhich.md | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/snippets/reduceWhich.md b/snippets/reduceWhich.md index 2ce6bb198..98ea5202e 100644 --- a/snippets/reduceWhich.md +++ b/snippets/reduceWhich.md @@ -1,25 +1,17 @@ ### reduceWhich - -By setting some rules, get Minimum or Maximum value in array . - -BTW, the Minimum / Maximum value could be `Object`, namely, the upper array could be a `Object` array(filled with `Object`). +Return minimum / maximum value of a array, after applying the provided / defaulted function to set comparing rule. +Use `Array.reduce()` with the first arguments and pass the above comparator function to `Array.reduce()` when running. ```js -const reduceWhich = (xs, comparator = (a, b) => a - b) => xs.reduce(function (a, b) { +const reduceWhich = (arr, comparator = (a, b) => a - b) => arr.reduce(function (a, b) { return comparator(a, b) >= 0 ? b : a; }); ``` ```js -// get minimum value from numeric array reduceWhich([1, 3, 2]); // 1 -// get maximum value from numeric array -reduceWhich([1, 3, 2], function (a, b) {return b - a;}); // 3 +reduceWhich([1, 3, 2], (a, b) => b - a); // 3 -// get minimum value from object array by a given key map to value -reduceWhich([{name: 'Tom', age: 12}, {name: 'Jack', age: 18}, {name: 'Lucy', age: 9}], function(a, b){return a.age - b.age;}) // {name: "Lucy", age: 9} - -// get maximum value from object array by a given key map to value -reduceWhich([{name: 'Tom', age: 12}, {name: 'Jack', age: 18}, {name: 'Lucy', age: 9}], function(a, b){return b.age - a.age;}) // {name: "Jack", age: 18} +reduceWhich([{name: 'Tom', age: 12}, {name: 'Jack', age: 18}, {name: 'Lucy', age: 9}], (a, b) => a.age - b.age) // {name: "Lucy", age: 9} ``` \ No newline at end of file