1、清空数组
const empty = (arr) => (arr.length = 0);
// Or
arr = [];
2、从数组中找到最接近的数字
// Find the number from `arr` which is closest to `n`
const closest = (arr, n) => arr.reduce((prev, curr) =>
(Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev));
// Or
const closest = (arr, n) => arr.sort((a, b) => Math.abs(a - n)
- Math.abs(b - n))[0];
example
closest([29, 87, 8, 78, 97, 20, 75, 33, 24, 17], 50); // 33
3、查找数组的最后一个匹配项的索引
const lastIndex = (arr, predicate) => arr.reduce
((prev, curr, index) => (predicate(curr) ? index : prev), -1);
// Or
const lastIndex = (arr, predicate) => arr.map((item)
=> predicate(item)).lastIndexOf(true);
Examples
lastIndex([1, 3, 5, 7, 9, 2, 4, 6, 8], (i) => i % 2 === 1); // 4
lastIndex([1, 3, 5, 7, 9, 8, 6, 4, 2], (i) => i > 6); // 5
4、查找数组最大项的索引
const indexOfMax = (arr) => arr.reduce((prev, curr, i, a)
=> (curr > a[prev] ? i : prev), 0);
Examples
indexOfMax([1, 3, 9, 7, 5]); // 2
indexOfMax([1, 3, 7, 7, 5]); // 2
5、查找数组中最长字符串的长度
const findLongest = (words) => Math.max(...words.map((el) => el.length));
Example
findLongest(['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']); // 6
6、通过给定键查找数组的最大项
const maxBy = (arr, key) => arr.reduce((a, b) =>
(a[key] >= b[key] ? a : b), {});
Example
const people = [
{ name: 'Bar', age: 24 },
{ name: 'Baz', age: 32 },
{ name: 'Foo', age: 42 },
{ name: 'Fuzz', age: 36 },
];
maxBy(people, 'age'); // { name: 'Foo', age: 42 }
7、 查找数组的最大项
const max = (arr) => Math.max(...arr);
8、通过给定键查找数组的最小项
const minBy = (arr, key) => arr.reduce((a, b)
=> (a[key] < b[key] ? a : b), {});
Example
const people = [
{ name: 'Bar', age: 24 },
{ name: 'Baz', age: 32 },
{ name: 'Foo', age: 42 },
{ name: 'Fuzz', age: 36 },
];
minBy(people, 'age'); // { name: 'Bar', age: 24 }
9、查找数组的最小项
const min = (arr) => Math.min(...arr);
10、展平数组
const flat = (arr) =>
[].concat.apply(
[],
arr.map((a) => (Array.isArray(a) ? flat(a) : a))
);
// Or
const flat = (arr) => arr.reduce((a, b) => (Array.isArray(b) ?
[...a, ...flat(b)] : [...a, b]), []);
// Or
// See the browser compatibility at https://caniuse.com/#feat=array-flat
const flat = (arr) => arr.flat();
flat(['cat', ['lion', 'tiger']]); // ['cat', 'lion', 'tiger']
11、根据指定元素数量拆数组
const getConsecutiveArrays = (arr, size) => (size > arr.length ?
[] : arr.slice(size - 1).map((_, i) => arr.slice(i, size + i)));
Examples
getConsecutiveArrays([1, 2, 3, 4, 5], 2); // [[1, 2], [2, 3], [3, 4], [4, 5]]
getConsecutiveArrays([1, 2, 3, 4, 5], 3); // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
getConsecutiveArrays([1, 2, 3, 4, 5], 6); // []
12、获取数组的所有第 n 项
const getNthItems = (arr, nth) => arr.filter
((_, i) => i % nth === nth - 1);
Examples
getNthItems([1, 2, 3, 4, 5, 6, 7, 8, 9], 2); // [2, 4, 6, 8]
getNthItems([1, 2, 3, 4, 5, 6, 7, 8, 9], 3); // [3, 6, 9]
13、 获取数组中某个值的索引
const indices = (arr, value) => arr.reduce((acc, v, i) =>
(v === value ? [...acc, i] : acc), []);
// Or
const indices = (arr, value) => arr.map((v, i) =>
(v === value ? i : false)).filter(Boolean);
Examples
indices(['h', 'e', 'l', 'l', 'o'], 'l'); // [2, 3]
indices(['h', 'e', 'l', 'l', 'o'], 'w'); // []