湘水狱 2022-10-18 17:00 采纳率: 0%
浏览 19

js filter数组去重

js filter方法 数组去重 哪位boss能讲解下原理或者思路

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-18 17:30
    关注
    • 这篇文章:JS中filter的使用 也许有你想要的答案,你可以看看
    • 除此之外, 这篇博客: js中数组filter方法的使用和实现中的 实现代码 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    •     Array.prototype.myFilter = function(callback, thisArg) {
              // 获取数组的长度,确定循环的次数
              const length = this.length
              let newArray = []
              for (let index = 0; index < length; index++) {
                  // 利用hasOwnProperty方法过滤原数组空值和已经删除的值
                  if (this.hasOwnProperty(index)) {
                      // 利用call方法改变callback函数this的指向
                      if (callback.call(thisArg, this[index], index, this)) newArray.push(this[index])
                  }
              }
              return newArray
          }
      
    评论

报告相同问题?

问题事件

  • 创建了问题 10月18日