Fix some issues
This commit is contained in:
@ -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}
|
||||
```
|
||||
Reference in New Issue
Block a user