企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## **编写一个方法 去掉一个数组的重复元素** ### 方法一: ``` var arr = [0,2,3,4,4,0,2]; var obj = {}; var tmp = []; for(var i = 0 ;i< arr.length;i++){ if( !obj[arr[i]] ){ obj[arr[i]] = 1; tmp.push(arr[i]); } } console.log(tmp); ``` * 结果如下: [0, 2, 3, 4] ###  方法二: ``` var arr = [2,3,4,4,5,2,3,6], arr2 = []; for(var i = 0;i< arr.length;i++){ if(arr2.indexOf(arr[i]) < 0){ arr2.push(arr[i]); } } console.log(arr2); ``` * 结果为:[2, 3, 4, 5, 6] ###  方法三: ``` var arr = [2,3,4,4,5,2,3,6]; var arr2 = arr.filter(function(element,index,self){ return self.indexOf(element) === index; }); console.log(arr2); ``` * 结果为:[2, 3, 4, 5, 6] ### 方法四: ``` var arr = [1,1,2,2,3,3,4]; var test = []; arr.forEach(ele=>{ if(!tesst.include(ele)){ test.push(ele) } }) console.log(test) ```