美文网首页
练习3--数组去重

练习3--数组去重

作者: 王二麻子88 | 来源:发表于2021-03-03 22:48 被阅读0次

输入

[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]

输出

[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a']

注意:本题的难度在于, 不需要去重 {}, 因此不建议使用一些骚操作, 正常来做进行了

Array.prototype.uniq = function (flag = false) {
  // 这个Flag = true 本身没有什么作用, 只是个人写法习惯
  let resultArr = []
  this.forEach(function(item ,index) {
    if (!resultArr.includes(item)) {
      resultArr.push(item);
    }
  });
  return resultArr;
}

相关文章

网友评论

      本文标题:练习3--数组去重

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