Array常见使用场景

参考:https://1loc.dev/

将一个值转为数组的方法
const castArray = (value) => (Array.isArray(value) ? value : [value]);
caseArray(1); // [1] 或直接使用Array.of(val)这个构造方法代替
检查数组是否为空数组
const isEmpty = (arr) => Array.isArray(arr) && !arr.length;
isEmpty([]); // true
isEmpty([1, 2, 3]); // false
不用考虑两个数组的顺序直接比较两个数组是否相同
// // `a` and `b` are arrays
const isEqual = (a, b) => JSON.stringify([...new Set(a)].sort()) === JSON.stringify([...new Set(b)].sort());
isEqual([1, 2, 3], [1, 2, 3]); // true
isEqual([], [1, 2]); // false
数组拷贝方式
const res = (arr) => arr.slice(0);
const res = (arr) => [...arr];
const res = (arr) => Array.from(arr);
将一个对象数组转为一个单一对象
const toObject = (arr, key) => arr.reduce((a, b) => ({ ...a, [b[key]]: b }), {});
const toObject = (arr, key) => Object.fromEntries(arr.map((it) => [it[key], it]));
将一组字符串转为真正数组
const toNumbers = (arr) => arr.map(Number);
const toNumbers = (arr) => arr.map((x) => +x);
toNumbers(['2', '3', '4']); // [2, 3, 4]
针对对象数组,根据某个字段统计相同值的个数
const countBy = (arr, prop) => arr.reduce((prev, curr) => ((prev[curr[prop]] = ++prev[curr[prop]] || 1), prev), {});
countBy(
[
{ branch: 'audi', model: 'q8', year: '2019' },
{ branch: 'audi', model: 'rs7', year: '2020' },
{ branch: 'ford', model: 'mustang', year: '2019' },
{ branch: 'ford', model: 'explorer', year: '2020' },
{ branch: 'bmw', model: 'x7', year: '2020' },
],
'branch'
);
// { 'audi': 2, 'ford': 2, 'bmw': 1 }
统计一个数组中某个值出现的次数
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
countOccurrences([2, 1, 3, 3, 2, 3], 2); // 2
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b'], 'a'); // 3
比较两个数组包含的内容是否相同
const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
isEqual([1, 2, 3], [1, 2, 3]); // true
isEqual([1, 2, 3], [1, '2', 3]); // false
统计一个数组中相同值出现的次数
const countOccurrences = (arr) => arr.reduce((prev, curr) => ((prev[curr] = ++prev[curr] || 1), prev), {});
countOccurrences([2, 1, 3, 3, 2, 3]); // { '1': 1, '2': 2, '3': 3 }
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b']); // { 'a': 3, 'b': 2, 'c': 1 }
寻找数组中最小值所在的索引位置
const indexOfMin = (arr) => arr.reduce((prev, curr, i, a) => (curr < a[prev] ? i : prev), 0);
indexOfMin([6, 4, 8, 2, 10]); // 3
indexOfMin([6, 4, 2, 2, 10]); // 2
数组对象中取得数组对象中其中一个字段的值最大的那个数组
const maxBy = (arr, key) => arr.reduce((a, b) => (a[key] >= b[key] ? a : b), {});
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 }
数组对象中取得数组对象中其中一个字段的值最小的那个数组
const minBy = (arr, key) => arr.reduce((a, b) => (a[key] < b[key] ? a : b), {});
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 }
数组扁平化
// 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']
获取一个数组中所有值的子集
const getSubsets = (arr) => arr.reduce((prev, curr) => prev.concat(prev.map((k) => k.concat(curr))), [[]]);
getSubsets([1, 2]); // [[], [1], [2], [1, 2]]
getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
获取数组的平均值
const average = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length;
获取两个数组之间的交集
const getIntersection = (a, ...arr) => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v)));
getIntersection([1, 2, 3], [2, 3, 4, 5]); // [2, 3]
getIntersection([1, 2, 3], [2, 3, 4, 5], [1, 3, 5]); // [3]
数组去重(得到非重复数组)
const unique = (arr) => [...new Set(arr)];
// Or
const unique = (arr) => arr.filter((el, i, array) => array.indexOf(el) === i);
数组分组处理(根据某个key对数组进行分组处理)
const groupBy = (arr, key) => arr.reduce((acc, item) => ((acc[item[key]] = [...(acc[item[key]] || []), item]), acc), {});
groupBy(
[
{ branch: 'audi', model: 'q8', year: '2019' },
{ branch: 'audi', model: 'rs7', year: '2020' },
{ branch: 'ford', model: 'mustang', year: '2019' },
{ branch: 'ford', model: 'explorer', year: '2020' },
{ branch: 'bmw', model: 'x7', year: '2020' },
],
'branch'
);
/*
{
audi: [
{ branch: 'audi', model: 'q8', year: '2019' },
{ branch: 'audi', model: 'rs7', year: '2020' }
],
bmw: [
{ branch: 'bmw', model: 'x7', year: '2020' }
],
ford: [
{ branch: 'ford', model: 'mustang', year: '2019' },
{ branch: 'ford', model: 'explorer', year: '2020' }
],
}
*/
移除数组中重复的值
const removeDuplicate = (arr) => arr.filter((i) => arr.indexOf(i) === arr.lastIndexOf(i));
removeDuplicate(['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']); // ['h', 'e', 'w', 'r', 'd']
合并并对数组去重
const union = (...arr) => [...new Set(arr.flat())];
union([1, 2], [2, 3], [3]); // [1, 2, 3]
posted @   Felix_Openmind  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
历史上的今天:
2022-02-09 加载额外配置文件的方式
*{cursor: url(https://files-cdn.cnblogs.com/files/morango/fish-cursor.ico),auto;}
点击右上角即可分享
微信分享提示