数组

75 阅读1分钟
  • 判断数组是否为空
        const isNotEmpty = arr => Array.isArray(arr) && arr.length > 0;

        isNotEmpty([1, 2, 3]); // true
  • 合并数组
        const merge = (a, b) => a.concat(b);

        const merge = (a, b) => [...a, ...b];
  • 单数组简单去重
        const removeDuplicates = arr => [...new Set(arr)];

        removeDuplicates([1, 2, 3, 3, 4, 5, 5]);
  • 双数组去重
    array_diff(a, b) {
        for (let i = 0; i < b.length; i++) {
            for (let j = 0; j < a.length; j++) {
                if (a[j].identityName == b[i].identityName) {
                    a.splice(j, 1);
                    j--;
                }
            }
        }
        return a;
    }