arrAndSet
并集
1 2 3 4 5 6 7 8
|
export const arrAndSet = (arrOne, arrTwo) => { return arrOne.concat(arrTwo.filter(v => !arrOne.includes(v))) }
|
arrIntersection
交集
1 2 3 4 5 6 7 8
|
export const arrIntersection = (arrOne, arrTwo) => { return arrOne.filter(v => arrTwo.includes(v)) }
|
arrDifference
差集
1 2 3 4 5 6 7 8 9
|
export const arrDifference = (arrOne, arrTwo) => { return arrOne.concat(arrTwo).filter(v => !arrOne.includes(v) || !arrTwo.includes(v)) }
|
arrTwoToArrObj
两个数组合并成一个数组对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
export const arrTwoToArrObj = (arrOne, arrTwo, oneKey, twoKey) => { if(!oneKey&&!twoKey){ return arrOne.map((oneKey, i) => ({ [oneKey]:arrTwo[i] })) }else{ return arrOne.map((oneKey, i) => ({ oneKey, twoKey: arrTwo[i] })) } }
|
arrObjSum
数组对象求和
1 2 3 4 5 6 7 8
|
export const arrObjSum = (obj, key) => { return arrObj.reduce((prev, cur) => prev + cur.key, 0) }
|
arrConcat
数组合并
1 2 3 4 5 6 7 8
|
export const arrConcat = (arrOne, arrTwo) => { return [...arrOne, ...arrTwo] }
|
arrSum
数组求和
1 2 3 4 5 6 7 8 9
|
export const arrSum = arr => { return arr.reduce((prev, cur)=> { return prev + cur }, 0) }
|
arrIncludeValue
数组是否包含某值
1 2 3 4 5 6 7 8
|
export const arrIncludeValue = (arr, value) => { return arr.includes( value) }
|
arrMax
数组最大值
1 2 3 4 5 6 7
|
export const arrMax = arr => { return Math.max(...arr) }
|
arrRemoveRepeat
数组去重
1 2 3 4 5 6 7
|
export const arrRemoveRepeat = arr => { return Array.from(new Set(arr)) }
|
arrOrderAscend
数组排序
1 2 3 4 5 6 7 8 9 10
|
export const arrOrderAscend = (arr, ascendFlag=true) => { return arr.sort((a, b) => { return ascendFlag ? a - b : b - a }) }
|
arrJudge
判断是否是数组
1 2 3 4 5 6 7 8 9
|
export const arrJudge = arr => { if (Array.isArray(arr)) { return true } }
|
作者:火狼1
链接:https://juejin.im/post/5de5be53f265da05c33fcbb4