美文网首页
set数组去重,判断数组是否为空,

set数组去重,判断数组是否为空,

作者: 空我我 | 来源:发表于2022-06-14 14:04 被阅读0次
  • 从数组中移除重复项
const removeDuplicates = (arr) => [...new Set(arr)];

console.log(removeDuplicates([1, 2, 2, 3, 3, 4, 4, 5, 5, 6]));

另一个办法去重

function dedupe(array){
    return Array.from(new Set(array));
}

dedupe([1,1,2,3]) //[1,2,3]
  • 判断数组是否为空
const isNotEmpty = arr => Array.isArray(arr) && arr.length > 0;

isNotEmpty([1, 2, 3]);  // true

相关文章

网友评论

      本文标题:set数组去重,判断数组是否为空,

      本文链接:https://www.haomeiwen.com/subject/nkdzmrtx.html