标签:asc 知识点 去重 == 复杂度 string script javascrip ===
const arr = [1, -1, NaN, 3, ‘a‘, 3, NaN, {name: ‘abc‘}, {name: ‘ABC‘}]
// ES5
arr.filter(function(item, index) {
return index === arr.indexOf(item)
}) // [1, -1, 3, "a", { name: ‘abc‘ }, { name: ‘ABC‘ }] 因为Array.prototype.indexOf(NaN) => -1
// ES5
var exists = {};
arr.filter(function(item) {
if (!exists[item]) {
exists[item] = true
return true
}
}) // [1, -1, NaN, 3, "a", { name: ‘abc‘}] 并且时间复杂度为O(n), { name: ‘abc‘ }与{ name: ‘ABC‘ }在这里重复的原因是方括号[]内会转化成字符串,Object.prototype.toString.call({name: ‘abc‘}) -> ‘[object Object]‘
// ES6
[...new Set(arr)] // [1, -1, NaN, 3, "a", { name: ‘abc‘ }, { name: ‘ABC‘ }]
// 知识点:ES6 Set以及Symbol.iterator
标签:asc 知识点 去重 == 复杂度 string script javascrip ===
原文地址:https://www.cnblogs.com/guanine/p/9216942.html