Recently, I have spent about a month on and off to reintegrate the tool classes collected by myself. Here, I have selected an array of 10 common methods to share, including project address and project document

1. Array union

const union = function (a, b, k) {
    return a.concat(b.filter(i= >(k ? ! a.map(i= >i[k]).includes(i[k]) : ! a.includes(i)))) }Copy the code

Example:

let a = [1.2.3.4.5]
let b = [1.2.4.5.6]
union(a, b) / / [6]

// Scenario 2:
let a1 = [
    { id: 1.name: 'Joe'.age: 20 },
    { id: 2.name: 'bill'.age: 21 },
    { id: 3.name: 'small 2'.age: 23}]let b1 = [
    { id: 2.name: 'bill'.age: 21 },
    { id: 4.name: 'Ming'.age: 24 },
    { id: 5.name: 'little red'.age: 25}]// Get the union by id
union(a1, b1, 'id')
/ * [{id: 1, name: "zhang", the age: 20} {id: 2, name: "bill", the age: 21} {id: 3, name: "small 2", the age: 23} {id: 4, name: "xiao Ming," the age: 24} {id: 5, name: "小红", age: 25}] */
Copy the code

2. Array intersection

const intersection = function (a, b, k) {
    return a.filter(t= > (k ? b.map(i= > i[k]).includes(t[k]) : b.includes(t)))
}
Copy the code

Example:

let a = [1.2.3.4.5]
let b = [1.2.4.5.6]
intersection(a, b) / /,2,4,5 [1]

// Scenario 2:
let a1 = [
    { id: 1.name: 'Joe'.age: 20 },
    { id: 2.name: 'bill'.age: 21 },
    { id: 3.name: 'small 2'.age: 23}]let b1 = [
    { id: 2.name: 'bill'.age: 21 },
    { id: 4.name: 'Ming'.age: 24 },
    { id: 5.name: 'little red'.age: 25 }
]
intersection(a1, b1, 'id') //[{id: 2, id: 2, age: 21}]
Copy the code

3. Array difference set

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))))
}
Copy the code

Example:

let a = [1.2.3.4.5]
let b = [1.2.4.5.6]

except(a, b) / / [3, 6]

let a1 = [
    { id: 1.name: 'Joe'.age: 20 },
    { id: 2.name: 'bill'.age: 21 },
    { id: 3.name: 'small 2'.age: 23}]let b1 = [
    { id: 2.name: 'bill'.age: 21 },
    { id: 4.name: 'Ming'.age: 24 },
    { id: 5.name: 'little red'.age: 25 }
]


except(a1, b1, 'id')
/ * [{id: 1, name: "zhang", the age: 20} {id: 3, name: "small 2", the age: 23} {id: 4, name: "xiao Ming," age: 24} {id: 5, name: "little red", the age: 25}] * /
Copy the code

4. Array grouping

/ * * *@description: One-dimensional array to two-dimensional array (grouping) *@param {Array} Arr: * an array@param {Number} Num: Cardinality of bisecting (num is grouped into groups (archiving)) */
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), [])
}
Copy the code

Example:

  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 and 6],,8,9 [7], [10]]
Copy the code

5. Array average

/** * array average *@param {Array} A: * an array@param {Function | String} F: function or 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
}
Copy the code

Example:

  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
Copy the code

6. Array generation

/ * * *@description: Generates an ascending array * between start and end digits, including the start and end digits@param {Number} Min: indicates the minimum *@param {Number} Max: indicates the maximum value */
const range = function (min, max) {
    return Array.from({ length: max - min + 1 }, (_, i) = > i + min)
}
Copy the code

Example:

 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]
 
Copy the code

7. Array summing

const sum = function (a, k) {
    return a.reduce((p, c) = > p + (k ? c[k] || 0 : c), 0)}Copy the code

Example:

let a = [1.2.3.4.5]
sum(a) / / 15

let a1 = [
    { id: 1.name: 'Joe'.age: 20 },
    { id: 2.name: 'bill'.age: 21 },
    { id: 3.name: 'small 2'.age: 23 }
]
sum(a1, 'age') / / 64
Copy the code

8. Array flattening

/** * specifies the depth flattening array *@param {Array} Arr: flattened array *@param {Number} Depth: flat level */
const flatten = function (arr, depth = 1) {
    return arr.reduce((a, v) = > a.concat(depth > 1 && Array.isArray(v) ? flatten(v, depth - 1) : v), [])
}
Copy the code

Example:

 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]]
Copy the code

9. Swap array values

/ * * *@description: Swaps the position of any two values in the array *@param {Array} Arr: * an array@param {Number} OldIndex: indicates the old location index *@param {Number} NewIndex: new location index *@param {Boolean} IsChangeOldArr: Whether to change the original array *@return {Array} Returns an 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
}
Copy the code

Example:

 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]
Copy the code

10. Array archiving

/ * * *@descriptionArchive a one-dimensional JSON array (by key) *@param {Array} Arr: one-dimensional array *@param {String} Key: The key character string is */
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), [])
}
Copy the code

Example:

let books = [ {date:'1 month'.name:'Geography book'}, {date:'1 month'.name:'History book'}, {date:'2 months'.name:'Chemistry Book'} ]

archive( books, 'date') 
/ / [[{date: 'in January, name:' geographical book '}, {date: 'in January, name:' history books'}], [{date: February, name: 'chemistry books'}]]
Copy the code

conclusion

If there are mistakes, please point them out and make progress together