请补全JavaScript代码,要求去除数组参数中的重复数字项并返回该数组。
注意:
1. 数组元素仅包含数字
const _deleteRepeat = array => { // 补全代码 // (1)使用ES6 中的set 方法进行去重 //return Array.from(new Set(array)) // (2)使用新数组对不重复的数组进行保存 let newArr = [] array.forEach(item => { if(newArr.indexOf(item) === -1){ newArr.push(item) } }); return newArr } console.log(_deleteRepeat([-1,1,2,2]));
<!DOCTYPE html> <html> <head> <meta charset=utf-8> </head> <body> <script type="text/javascript"> const _deleteRepeat = array => { // 补全代码 return array.filter((item, index, arr) => arr.indexOf(item) === index) return [...new Set(array)] return Array.from(new Set(array)) } </script> </body> </html>
const _deleteRepeat = array => { // 补全代码 return array.filter((el,index) => { return array.findIndex(item => el == item) == index }) }