整点简单的,分享10个数组工具类
编程微刊
共 4909字,需浏览 10分钟
·
2021-06-24 06:41
1.数组并集
const union = function (a, b, k) {
return a.concat(b.filter(i => (k ? !a.map(i => i[k]).includes(i[k]) : !a.includes(i))))
}
复制代码
示例:
let a = [1, 2, 3, 4, 5]
let b = [1, 2, 4, 5, 6]
union(a, b) //[1,2,3,4,5,6]
// 场景2:
let a1 = [
{ id: 1, name: '张三', age: 20 },
{ id: 2, name: '李四', age: 21 },
{ id: 3, name: '小二', age: 23 }
]
let b1 = [
{ id: 2, name: '李四', age: 21 },
{ id: 4, name: '小明', age: 24 },
{ id: 5, name: '小红', age: 25 }
]
// 通过 id 获取并集
union(a1, b1, 'id')
/*
[
{id: 1, name: "张三", age: 20}
{id: 2, name: "李四", age: 21}
{id: 3, name: "小二", age: 23}
{id: 4, name: "小明", age: 24}
{id: 5, name: "小红", age: 25}
]
*/
复制代码
2.数组交集
const intersection = function (a, b, k) {
return a.filter(t => (k ? b.map(i => i[k]).includes(t[k]) : b.includes(t)))
}
复制代码
示例:
let a = [1, 2, 3, 4, 5]
let b = [1, 2, 4, 5, 6]
intersection(a, b) // [1,2,4,5]
// 场景2:
let a1 = [
{ id: 1, name: '张三', age: 20 },
{ id: 2, name: '李四', age: 21 },
{ id: 3, name: '小二', age: 23 }
]
let b1 = [
{ id: 2, name: '李四', age: 21 },
{ id: 4, name: '小明', age: 24 },
{ id: 5, name: '小红', age: 25 }
]
intersection(a1, b1, 'id') //[ { id: 2, name: '李四', age: 21 }]
复制代码
3.数组差集
const except = function (a, b, k) {
return [...a, ...b].filter(i => ![a, b].every(t => (k ? t.map(i => i[k]).includes(i[k]) : t.includes(i))))
}
复制代码
示例:
let a = [1, 2, 3, 4, 5]
let b = [1, 2, 4, 5, 6]
except(a, b) // [3,6]
let a1 = [
{ id: 1, name: '张三', age: 20 },
{ id: 2, name: '李四', age: 21 },
{ id: 3, name: '小二', age: 23 }
]
let b1 = [
{ id: 2, name: '李四', age: 21 },
{ id: 4, name: '小明', age: 24 },
{ id: 5, name: '小红', age: 25 }
]
except(a1, b1, 'id')
/*
[
{id: 1, name: "张三", age: 20}
{id: 3, name: "小二", age: 23}
{id: 4, name: "小明", age: 24}
{id: 5, name: "小红", age: 25}
]
*/
复制代码
4.数组分组
/**
* @description: 一维数组转二维数组 (分组)
* @param {Array} arr:数组
* @param {Number} num: 平分基数(num 个为一组进行分组(归档))
*/
const group = function (arr, num) {
return [...Array(Math.ceil(arr.length / num)).keys()].reduce((p, _, i) => (p.push(arr.slice(i * num, (i + 1) * num)), p), [])
}
复制代码
示例:
group([1,2,3,4,5,6,7,8,9,10],2) // [[1,2],[3,4],[5,6],[7,8],[9.10]]
group([1,2,3,4,5,6,7,8,9,10],3) // [[1,2,3],[4,5,6],[7,8,9],[10]]
复制代码
5.数组平均数
/**
* 数组平均数
* @param {Array} a:数组
* @param {Function | String} f:函数 或 key
*/
const mean = function (a, f) {
return (f ? a.map(typeof f === 'function' ? f : v => v[f]) : a).reduce((acc, val) => acc + val * 1, 0) / a.length
}
复制代码
示例:
mean([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n) // 5
mean([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n') // 5
mean([4, 2, 8, 6]) // 5
mean(['4', 2, '8', 6]) // 5
复制代码
6.数组生成
/**
* @description: 生成 起止数字间(包含起止数字)的升序数组
* @param {Number} min : 最小值
* @param {Number} max :最大值
*/
const range = function (min, max) {
return Array.from({ length: max - min + 1 }, (_, i) => i + min)
}
复制代码
示例:
range(0,10) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(1,9) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
复制代码
7.数组求和
const sum = function (a, k) {
return a.reduce((p, c) => p + (k ? c[k] || 0 : c), 0)
}
复制代码
示例:
let a = [1, 2, 3, 4, 5]
sum(a) // 15
let a1 = [
{ id: 1, name: '张三', age: 20 },
{ id: 2, name: '李四', age: 21 },
{ id: 3, name: '小二', age: 23 }
]
sum(a1, 'age') // 64
复制代码
8.数组扁平化
/**
* 指定深度扁平化数组
* @param {Array} arr :扁平化的数组
* @param {Number} depth:扁平化的层级
*/
const flatten = function (arr, depth = 1) {
return arr.reduce((a, v) => a.concat(depth > 1 && Array.isArray(v) ? flatten(v, depth - 1) : v), [])
}
复制代码
示例:
flatten([1, 2, 3, [4, [5, 6, [7]]]]) //[1, 2, 3, 4, [5,6,[7]]]
flatten([1, 2, 3, [4, [5, 6, [7]]]], 2) //[1, 2, 3, 4, 5,6,[7]]
复制代码
9. 数组值位置交换
/**
* @description: 交换数组中任一两个值的位置
* @param {Array} arr:数组
* @param {Number} oldIndex:老位置索引
* @param {Number} newIndex:新位置索引
* @param {Boolean} isChangeOldArr: 是否改变原数组
* @return {Array} 返回一个数组
*/
const exchangePostion = function (arr, oldIndex, newIndex, isChangeOldArr = false) {
let a = isChangeOldArr ? arr : JSON.parse(JSON.stringify(arr))
a.splice(oldIndex, 1, a.splice(newIndex, 1, a[oldIndex])[0])
return a
}
复制代码
示例:
let a1 = [1, 2, 3, 4, 5, 6]
exchangePostion(a1, 4, 1)// [1, 5, 3, 4, 2, 6]
a1 //[1, 2, 3, 4, 5, 6]
let a1 = [1, 2, 3, 4, 5, 6]
exchangePostion(a1, 4, 1,true)// [1, 5, 3, 4, 2, 6]
a1 // [1, 5, 3, 4, 2, 6]
复制代码
10.数组归档
/**
* @description: 对一维 json 数组进行归档(根据 key)
* @param {Array} arr:一维数组
* @param {String} key:key 字符串
*/
const archive = function (arr, key) {
return Array.from(new Set(arr.map(i => i[key]))).reduce((p, c) => (p.push(arr.filter(i => i[key] === c)), p), [])
}
复制代码
示例:
let books = [ {date:'1月',name:'地理书'}, {date:'1月',name:'历史书'}, {date:'2月',name:'化学书'} ]
archive( books, 'date')
// [[{date:'1月',name:'地理书'},{date:'1月',name:'历史书'}],[ {date:'2月',name:'化学书'}]]
复制代码
转自:https://juejin.cn/post/6975846295059562503
评论