ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
```js var array = [3, 7, 1, 3, 7, 1, 2, 4, 3, 7, 5]; ``` ### for循环遍历法 ```js function has(array, val) { for (var i = 0, len = array.length; i < len; i++) { if (array[i] === val) return true; } return false; } var newArray = []; for (var i = 0, len = array.length; i < len; i++) { var curr = array[i]; if (!has(newArray, curr)) newArray.push(curr); } console.log(newArray) ``` ***** ### IndexOf ```js var newArray = []; for (var i = 0, len = array.length; i < len; i++) { if (newArray.indexOf(array[i]) === -1) newArray.push(array[i]); } console.log(newArray) ``` ***** ### includes ```js var newArray = array.reduce(function (result, current) { if (!result.includes(current)) result.push(current); return result; }, []); console.log(newArray) ``` ### New Set ***** ```js var set = new Set(); array.forEach(function (curr) { set.add(curr); }); var newArray = Array.from(set); console.log(newArray); ``` ***** ### From ```js var newArray = Array.from(new Set(array)); console.log(newArray); ```