企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
#### **1、调用indexOf方法** ~~~ var arr = [12, 1, 1, 11, 64, 64, 78, 9]; function unique2(arr) { var hash = []; for (var i = 0; i < arr.length; i++) { if (arr.indexOf(arr[i]) == i) { hash.push(arr[i]); } } return hash; } console.log(unique2(arr)); // [12, 1, 11, 64, 78, 9] ~~~ #### **2.内置对象 includes:** ~~~ var a = [11,11,22,44,33,33]; var temp = []; a.forEach(element =>{ if(!temp.includes(element)){ temp.push(element) } }) console.log(temp); ~~~ #### **3.es6新加方法:** ``` var a = [11,11,22,44,33,33]; console.log([...new Set(a)]) ``` #### **4.obj 方法** ``` var a = [11,11,22,44,33,33]; var temp = []; var obj = {}; a.forEach(ele => { if(!obj[ele]){ obj[ele]= 'a' ; temp.push(ele); } }) console.log(obj); //{11: "a", 22: "a", 33: "a", 44: "a"} console.log(temp); // [11, 22, 44, 33] ``` [还有太多办法了,但是我不想背了,这是地址你去看吧](https://www.cnblogs.com/jiayuexuan/p/7527055.html)