js filter数组去重

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

  • 这篇文章: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
        }