Fix some issues

This commit is contained in:
liuliangsir
2018-01-25 18:33:16 +08:00
parent a4ac6b45e3
commit e750c8e19c

View File

@ -1,25 +1,17 @@
### reduceWhich ### reduceWhich
Return minimum / maximum value of a array, after applying the provided / defaulted function to set comparing rule.
By setting some rules, get Minimum or Maximum value in array . Use `Array.reduce()` with the first arguments and pass the above comparator function to `Array.reduce()` when running.
BTW, the Minimum / Maximum value could be `Object`, namely, the upper array could be a `Object` array(filled with `Object`).
```js ```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; return comparator(a, b) >= 0 ? b : a;
}); });
``` ```
```js ```js
// get minimum value from numeric array
reduceWhich([1, 3, 2]); // 1 reduceWhich([1, 3, 2]); // 1
// get maximum value from numeric array reduceWhich([1, 3, 2], (a, b) => b - a); // 3
reduceWhich([1, 3, 2], function (a, b) {return 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}], (a, b) => a.age - b.age) // {name: "Lucy", age: 9}
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}
``` ```