js单行代码处理数据汇总 ———— 数组篇(二)

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'); // []

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenshuai5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值