<html>
<body>
<script>
Array.prototype.removeRepeatItem = function () {
let len = this.length;
let obj = {};
for (let i = 0; i < len; i++) {
if (!obj[this[i] + typeof i]) {
obj[this[i] + '*' + typeof this[i]] = this[i];
}
}
let result = [];
for (let key in obj) {
result.push(obj[key]);
}
console.log(obj);
return result;
}
let myArr = [1, 1, 2, 1, '1', true, 'true', 2, 'abc', 'true', true, false, {}, {}, null, undefined, 'null', 'undefined'];
console.log(myArr.removeRepeatItem());
</script>
</body>
</html>
- 采用对象存储的形式
- key用元素+*+元素类型的方式命名,value是对应的数组元素
- 没有就创建