We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
在ES6中对set的定义是: Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用, 因此我们可以利用这个特性来是实现去重
const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5] console.log([...new Set(numbers)]) // [2, 3, 4, 5, 6, 7, 32]
const object = {} const array = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5] array.map((number) => { object[number] = true }) // 这一步object的key将都被默认设置为字符串 console.log(object) let arrayStr = Object.keys(object) // 便利将每项的字符串转化为10进制数字 arrayStr.map((s) => parseInt(s, 10))
去重其实应该是我们经常会用到,第一反应就是使用Set就好了(IE11以上都是支持Set的),至于第二种方法的了解对我们日常开发貌似也并没有什么卵用,但是当我看到第二种方式的实现的时候,我还是很想把它记录下来。
object[number] = true
希望保持一颗沉淀的心
The text was updated successfully, but these errors were encountered:
No branches or pull requests
一、使用ES6属性Set去重
在ES6中对set的定义是: Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用,
因此我们可以利用这个特性来是实现去重
二、其他方式去重
思考
去重其实应该是我们经常会用到,第一反应就是使用Set就好了(IE11以上都是支持Set的),至于第二种方法的了解对我们日常开发貌似也并没有什么卵用,但是当我看到第二种方式的实现的时候,我还是很想把它记录下来。
object[number] = true
用对象下标的形式实现相当巧妙,nice希望保持一颗沉淀的心
The text was updated successfully, but these errors were encountered: