Blog icon indicating copy to clipboard operation
Blog copied to clipboard

《JavaScript专题之数组去重》勘误 Thanks MR-YUZL

Open MR-YUZL opened this issue 3 years ago • 2 comments

image

键值对去重,正则会被JSON.stringify转成{},导致/a/和/b/没有区别,去重之后只会保留一个正则

image

MR-YUZL avatar Jun 02 '21 11:06 MR-YUZL

@MR-YUZL 非常感谢你的指正,这里确实存在问题,相关的文章已引用更正

mqyqingfeng avatar Nov 14 '21 01:11 mqyqingfeng

function unique(array) {
    var obj = {};
    return array.filter(function (item, index, array) {
        console.log(typeof item + item + JSON.stringify(item))
        return obj.hasOwnProperty(typeof item + item + JSON.stringify(item)) ? false : (obj[typeof item + item + JSON.stringify(item)] = true)
    })
}
改成这样可以解决你说的正则不能去重的问题吗

fefuns avatar Mar 12 '22 20:03 fefuns