JavaScript去掉数组中的重复元素
程序员文章站
2022-04-12 13:27:35
...
题目:去掉数组[4,3,"3",3,5,7,5]中的重复元素,返回[4,3,"3",5,7]
(function() { 'use strict'; function filter1(arr) { var b = []; arr.forEach(function(i) { if (b.indexOf(i) == -1) { b.push(i); } }); return b; } function filter2(arr) { var b = {}, c = []; arr.forEach(function(i) { b[i] = b[i] ? b[i] : {}; var type = typeof i; if (!b[i][type]) { b[i][type] = true; c.push(i); } }); return c; } function timer(fn, arr) { console.time('filter'); fn.call(this, arr); console.timeEnd('filter'); } function testArr(n) { // var arr = [4,3,"3",3,5,7,5]; var arr = []; for (var i = 0; i