原文连接:yazeedb.com/posts/learn…
原做者:Yazeed Bzadough数组
在上一篇文章中,我向你提出了使用reduce
去建立一些众所周知的函数。本文将向你展现如何实现其中的一些,另外还有一些其它的函数。函数
总的来讲,咱们将研究10个实用的函数。它们在您的项目上很是方便,并且最重要的是,它们是用reduce
来实现的!我从RamdaJS库中得到了不少灵感,看看吧!post
predicate
- 返回 true
或 false
的函数array
- 要测试的列表若是任一项的 predicate
返回 true
, some
返回 true
,不然返回 false
。测试
const some = (predicate, array) => array.reduce((acc, value) => acc || predicate(value), false);
复制代码
const equals3 = (x) => x === 3;
some(equals3, [3]); // true
some(equals3, [3, 3, 3]); // true
some(equals3, [1, 2, 3]); // true
some(equals3, [2]); // false
复制代码
predicate
- 返回 true
或 false
的函数array
- 要测试的列表若是每一项的 predicate
都返回 true
, all
返回 true
,不然返回 false
。spa
const all = (predicate, array) => array.reduce((acc, value) => acc && predicate(value), true);
复制代码
const equals3 = (x) => x === 3;
all(equals3, [3]); // true
all(equals3, [3, 3, 3]); // true
all(equals3, [1, 2, 3]); // false
all(equals3, [3, 2, 3]); // false
复制代码
predicate
- 返回 true
或 false
的函数array
- 要测试的列表若是每一项的 predicate
都返回 false
, none
返回 true
,不然返回 false
。code
const none = (predicate, array) => array.reduce((acc, value) => acc && !predicate(value), true);
复制代码
const isEven = (x) => x % 2 === 0;
none(isEven, [1, 3, 5]); // true
none(isEven, [1, 3, 4]); // false
none(equals3, [1, 2, 4]); // true
none(equals3, [1, 2, 3]); // false
复制代码
transformFunction
- 在每一个元素上运行的函数array
- 要转换的列表返回一个新数组,每一项都通过了 transformFunction
的转换orm
const map = (transformFunction, array) =>
array.reduce((newArray, item) => {
newArray.push(transformFunction(item));
return newArray;
}, []);
复制代码
const double = (x) => x * 2;
const reverseString = (string) =>
string
.split('')
.reverse()
.join('');
map(double, [100, 200, 300]);
// [200, 400, 600]
map(reverseString, ['Hello World', 'I love map']);
// ['dlroW olleH', 'pam evol I']
复制代码
predicate
- 返回 true
或 false
的函数array
- 要过滤的列表返回一个新数组。若是 predicate
返回 true
,则将该元素添加至新数组中,不然该元素将被排除在新数组以外。对象
const filter = (predicate, array) =>
array.reduce((newArray, item) => {
if (predicate(item) === true) {
newArray.push(item);
}
return newArray;
}, []);
复制代码
const isEven = (x) => x % 2 === 0;
filter(isEven, [1, 2, 3]);
// [2]
filter(equals3, [1, 2, 3, 4, 3]);
// [3, 3]
复制代码
predicate
- 返回 true
或 false
的函数array
- 要过滤的列表就像 filter
,但行为相反。ip
若是 predicate
返回 false
,则将该元素添加至新数组中,不然该元素将被排除在新数组以外。get
const reject = (predicate, array) =>
array.reduce((newArray, item) => {
if (predicate(item) === false) {
newArray.push(item);
}
return newArray;
}, []);
复制代码
const isEven = (x) => x % 2 === 0;
reject(isEven, [1, 2, 3]);
// [1, 3]
reject(equals3, [1, 2, 3, 4, 3]);
// [1, 2, 4]
复制代码
predicate
- 返回 true
或 false
的函数array
- 要查找的列表返回第一个与 predicate
匹配到的元素,若是没有匹配的则返回 undefined
const find = (predicate, array) =>
array.reduce((result, item) => {
if (result !== undefined) {
return result;
}
if (predicate(item) === true) {
return item;
}
return undefined;
}, undefined);
复制代码
const isEven = (x) => x % 2 === 0;
find(isEven, []); // undefined
find(isEven, [1, 2, 3]); // 2
find(isEven, [1, 3, 5]); // undefined
find(equals3, [1, 2, 3, 4, 3]); // 3
find(equals3, [1, 2, 4]); // undefined
复制代码
predicate
- 返回 true
或 false
的函数array
- 列表基于 predicate
把一个数组“划分”或者分割成两部分,若是 predicate
返回 `true``,该元素进入列表1,不然进入列表2
const partition = (predicate, array) =>
array.reduce(
(result, item) => {
const [list1, list2] = result;
if (predicate(item) === true) {
list1.push(item);
} else {
list2.push(item);
}
return result;
},
[[], []]
);
复制代码
const isEven = (x) => x % 2 === 0;
partition(isEven, [1, 2, 3]);
// [[2], [1, 3]]
partition(isEven, [1, 3, 5]);
// [[], [1, 3, 5]]
partition(equals3, [1, 2, 3, 4, 3]);
// [[3, 3], [1, 2, 4]]
partition(equals3, [1, 2, 4]);
// [[], [1, 2, 4]]
// [1, 2, 4]
复制代码
key
- 从对象中选取的键名array
- 列表从数组中的每一个项目中抽取给定 key
的值。返回这些值组成新数组。
const pluck = (key, array) =>
array.reduce((values, current) => {
values.push(current[key]);
return values;
}, []);
复制代码
pluck('name', [{ name: 'Batman' }, { name: 'Robin' }, { name: 'Joker' }]);
// ['Batman', 'Robin', 'Joker']
pluck(0, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
// [1, 4, 7]
复制代码
reducer
- 标准的 rducer
函数,它接收两个参数 - 数组的累加器和当前值。initialValue
- 累加器的初始值array
- 列表它的工做原理和 reduce
类似,不一样的是 reduce
只返回最后结果,而 scan
返回由每次累加器结果组成的数组。
const scan = (reducer, initialValue, array) => {
const reducedValues = [];
array.reduce((acc, current) => {
const newAcc = reducer(acc, current);
reducedValues.push(newAcc);
return newAcc;
}, initialValue);
return reducedValues;
};
复制代码
const add = (x, y) => x + y;
const multiply = (x, y) => x * y;
scan(add, 0, [1, 2, 3, 4, 5, 6]);
// [1, 3, 6, 10, 15, 21] - Every number added from 1-6
scan(multiply, 1, [1, 2, 3, 4, 5, 6]);
// [1, 2, 6, 24, 120, 720] - Every number multiplied from 1-6
复制代码