数组 Array

数组去重

function noRepeat(arr) {
  return [...new Set(arr)]
}

数组对象去重

const arr = [
    { id: 1, value: 111 },
    { id: 2, value: 222 },
    { id: 3, value: 333 },
    { id: 1, value: 111 }
];

// 方法1:利用对象访问属性的方法,判断对象中是否存在key
function singleObjArr(arr, key = 'id') {
    const result = [];
    const obj = {};
    for (let item of arr) {
        if (!obj[item[`${key}`]]) {
            result.push(item)
            obj[item[`${key}`]] = true
        }
    }
    return result
}

// 方法2:利用`reduce`方法遍历数组,`reduce`第一个参数是遍历需要执行的函数,第二个参数是item的初始值
function singleObjArr(arr, key = 'id') {
    const obj = {}
    arr = arr.reduce(function (item, next) {
        obj[next[`${key}`]] ? '' : obj[next[`${key}`]] = true && item.push(next);
        return item;
    }, [])

    return arr
}

查找数组最大

function arrayMax(arr) {
  return Math.max(...arr);
}

查找数组最小

function arrayMin(arr) {
  return Math.min(...arr);
}

返回已size为长度的分割后的数组

function chunk(arr, size = 1) {
  return Array.from({
    length: Math.ceil(arr.length / size)
  }, (v, i) => arr.slice(i * size, i * size + size));
}

检查数组中某元素出现的次数

function countOccurrences(arr, value) {
  return arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
}

扁平化数组

  • 默认depth全部展开
function flatten(arr, depth = -1) {
  if (depth === -1) {
    return [].concat(...arr.map(v => Array.isArray(v) ? flatten(v) : v))
  }
  if (depth === 1) {
    return arr.reduce((a, v) => a.concat(v), []);
  }
  return arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), [])
}

对比两个数组并且返回其中不同的元素

function diffrence(arrA, arrB) {
  return arrA.filter(v => !arrB.includes(v))
}

返回两个数组中相同的元素

function intersection(arr1, arr2) {
  return arr2.filter(v => arr1.includes(v))
}

从右删除n个元素

function dropRight(arr, n = 0) {
  return n < arr.length ? arr.slice(0, arr.length - n) : [];
}

截取第一个符合条件的元素及其以后的元素

function dropElements(arr, fn) {
  while (arr.length && !fn(arr[0])) arr = arr.slice(1);
  return arr;
}

返回数组中下标间隔nth的元素

function everyNth(arr, nth) {
  return arr.filter((v, i) => i % nth === nth - 1)
}

返回数组中第n个元素

  • 支持负数
function nthElement(arr, n = 0) {
  return (n >= 0 ? arr.slice(n, n + 1) : arr.slice(n))[0]
}

返回数组头元素

function head(arr) {
  return arr[0]
}

返回数组末尾元素

function last(arr) {
  return arr[arr.length - 1]
}

数组乱排

function shuffle(arr) {
  let array = arr
  let index = array.length

  while (index) {
    index -= 1
    let randomInedx = Math.floor(Math.random() * index)
    let middleware = array[index]
    array[index] = array[randomInedx]
    array[randomInedx] = middleware
  }

  return array
}

过滤数组中的无效值

const arrFilter = [undefined, null, "", 0, false, NaN, 1, 2].filter(Boolean);

// arr => [1, 2]

数组reduce的高级用法

// 累加累乘
function Accumulation(...vals) {
    return vals.reduce((t, v) => t + v, 0);
}

function Multiplication(...vals) {
    return vals.reduce((t, v) => t * v, 1);
}

Accumulation(1, 2, 3, 4, 5); // 15
Multiplication(1, 2, 3, 4, 5); // 120

// 权重求和

const scores = [
    { score: 90, subject: "chinese", weight: 0.5 },
    { score: 95, subject: "math", weight: 0.3 },
    { score: 85, subject: "english", weight: 0.2 }
];
const result = scores.reduce((t, v) => t + v.score * v.weight, 0); // 90.5

// 实现数组reverse
function Reverse(arr = []) {
    return arr.reduceRight((t, v) => (t.push(v), t), []);
}

Reverse([1, 2, 3, 4, 5]); // [5, 4, 3, 2, 1]

// 数组分割
function Chunk(arr = [], size = 1) {
    return arr.length ? arr.reduce((t, v) => (t[t.length - 1].length === size ? t.push([v]) : t[t.length - 1].push(v), t), [[]]) : [];
}

const arr = [1, 2, 3, 4, 5];
Chunk(arr, 2); // [[1, 2], [3, 4], [5]]

// 数组扁平
function Flat(arr = []) {
    return arr.reduce((t, v) => t.concat(Array.isArray(v) ? Flat(v) : v), [])
}

const arr = [0, 1, [2, 3], [4, 5, [6, 7]], [8, [9, 10, [11, 12]]]];
Flat(arr); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

// 数组去重
function Uniq(arr = []) {
    return arr.reduce((t, v) => t.includes(v) ? t : [...t, v], []);
}

const arr = [2, 1, 0, 3, 2, 1, 2];
Uniq(arr); // [2, 1, 0, 3]

// 数组成员个数统计
function Count(arr = []) {
    return arr.reduce((t, v) => (t[v] = (t[v] || 0) + 1, t), {});
}

const arr = [0, 1, 1, 2, 2, 2];
Count(arr); // { 0: 1, 1: 2, 2: 3 }

// 数组成员位置记录
function Position(arr = [], val) {
    return arr.reduce((t, v, i) => (v === val && t.push(i), t), []);
}

const arr = [2, 1, 5, 4, 2, 1, 6, 6, 7];
Position(arr, 2); // [0, 4]

// 数组成员特性分组
function Group(arr = [], key) {
    return key ? arr.reduce((t, v) => (!t[v[key]] && (t[v[key]] = []), t[v[key]].push(v), t), {}) : {};
}

const arr = [
    { area: "GZ", name: "YZW", age: 27 },
    { area: "GZ", name: "TYJ", age: 25 },
    { area: "SZ", name: "AAA", age: 23 },
    { area: "FS", name: "BBB", age: 21 },
    { area: "SZ", name: "CCC", age: 19 }
]; // 以地区area作为分组依据
Group(arr, "area"); // { GZ: Array(2), SZ: Array(2), FS: Array(1) }

// URL参数序列化
function StringifyUrlSearch(search = {}) {
    return Object.entries(search).reduce(
        (t, v) => `${t}${v[0]}=${encodeURIComponent(v[1])}&`,
        Object.keys(search).length ? "?" : ""
    ).replace(/&$/, "");
}

StringifyUrlSearch({ age: 27, name: "YZW" }); // "?age=27&name=YZW"

// URL参数反序列化
function ParseUrlSearch() {
    return location.search.replace(/(^\?)|(&$)/g, "").split("&").reduce((t, v) => {
        const [key, val] = v.split("=");
        t[key] = decodeURIComponent(val);
        return t;
    }, {});
}

// 假设URL为:https://www.baidu.com?age=25&name=TYJ
ParseUrlSearch(); // { age: "25", name: "TYJ" }

// 返回对象指定键值
function GetKeys(obj = {}, keys = []) {
    return Object.keys(obj).reduce((t, v) => (keys.includes(v) && (t[v] = obj[v]), t), {});
}

const target = { a: 1, b: 2, c: 3, d: 4 };
const keyword = ["a", "d"];
GetKeys(target, keyword); // { a: 1, d: 4 }

判断数组中的元素是否都相等

const allEqual = arr => arr.every(val => val === arr[0]);

allEqual([1, 2, 3, 4, 5, 6]); // false
allEqual([1, 1, 1, 1]); // true
上次更新时间: 2020-05-18 06:28:00