Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数组去重 #38

Open
jangdelong opened this issue Aug 25, 2020 · 2 comments
Open

数组去重 #38

jangdelong opened this issue Aug 25, 2020 · 2 comments
Labels

Comments

@jangdelong
Copy link
Contributor

jangdelong commented Aug 25, 2020

将数组 [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}] 进行去重,去重结果为 [1, 'true', true,15,false, undefined, null, NaN, 'NaN', 0, 'a',{}]

题目有点小变态,使用常规的简单去重方法应该是搞不赢。

@jangdelong
Copy link
Contributor Author

jangdelong commented Aug 26, 2020

方法一:

function unique (arr) {
  return [ ...new Set(arr) ]
}

缺点:对象{}无法去重。

@kaiziwen
Copy link

kaiziwen commented Nov 22, 2020

function unique (arr) {
    var obj = {}
    return arr.filter(item => {
        return obj.hasOwnProperty(typeof item + item) ? false : obj[typeof item + item] = true
    })
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants