數組工具方法

// https://30secondsofcode.org/index

// 1數組是否全面知足條件
const all = (arr, fn = Boolean) => arr.every(fn);
all([4, 2, 3], x => 1); // true

// 2.檢查全部元素是否與第一個元素相等
const allEqual = arr => arr.every(val => val === arr[0]);
allEqual([1, 2, 3]); // false allEqual([1,1,1]) // true

// 3.數組中只有有一個知足條件
const any = (arr, fn = Boolean) => arr.some(fn);
any([0, 1, 2, 0], x => x >= 2); // true

// 4.數組分爲多個chunk
const chunk = (arr, size) => {
  Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => {
    arr.slice(i * size, i * size + size);
  });
};
chunk([1, 2, 3, 4, 5], 2); // [[1,2],[3,4],5]

// 5.返回值爲真的數組
const compact = arr => arr.filter(Boolean);
compact([0, 1, false, '', Nan]); // [0,1]

// 6.統計給定值在數組中出現的次數
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
countOccurrences([1, 2, 3, 4, 1, 2, 1], 1); // 3

// 7.數組多維變一維
const deepFlatten = arr => [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v)));
deepFlatten([1, 2, [3, 4, [, 5]]]); // [1,2,3,4,5]

// 8.數組b在數組a中的差幾
const difference = (a, b) => {
  const s = new Set(b);
  return a.filter(x => !s.has(x));
};
difference([1, 2, 3], [1, 2, 4]); // 3

// 9數組扁平到指定的維度
const flatten = (arr, depth = 1) => arr.reduce((a, v) => a.concat(depth > 1 && Array.isArray(v) ? flatten(v, depth - 1) : v), []);
flatten([1, [2, [3, [4, 5], 6], 7], 8], 2); // [1, 2, 3, [4, 5], 6, 7, 8]

// 10數組中的交集部分
const intersection = (a, b) => {
  const s = new Set(b);
  return s.filter(x => s.has(x));
};
intersection([1, 2, 3], [4, 3, 2]); // [2,3]

// 11過濾數組中指定的值
const pull = (arr, ...agrs) => {
  let argState = Array.isArray(args[0]) ? args[0] : args;
  let pulled = arr.filter((v, i) => !argState.includes(v))
  arr.length = 0;
  pulled.forEach(v => {
    arr.push(v)
  });
}
let = myArray = ['a', 'b', 'c', 'a', 'b', 'c']
pull(myArray, 'a', 'b') // [b, c]
複製代碼
相關文章
相關標籤/搜索