分享10個數組工具類

最近斷斷續續花了1個月左右,把以前本身整理收集的工具類從新整合了下,在此特選出 其中數組 10 個經常使用方法來分享,項目地址項目文檔html

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))))
}
複製代碼

示例:git

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)))
}
複製代碼

示例:github

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))))
}
複製代碼

示例:json

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
}
複製代碼

示例:markdown

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), [])
}
複製代碼

示例:oop

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
}
複製代碼

示例:ui

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, 1true)// [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:'化學書'}]]
複製代碼

結語

若有錯誤的地方,歡迎指出,共同進步

相關文章
相關標籤/搜索