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:'化学书'}]]
结语
如有错误的地方,欢迎指出,共同进步
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!