数组的去重

2019-07-01  本文已影响0人  为了_理想
        let array = [1, 2, 2,3,4,5,6,7,7,8,9];
          function de_Weight (array) {
            let temp = []; //一个新的临时数组
            for (let i = 0; i < array.length; i++) {
                if (temp.indexOf(array[i]) == -1) {
                    temp.push(array[i]);
                }
            }
            return temp;
        }
     let index = de_Weight(array)
    console.log(index);  //1 2 3 4 5 6 7 8 9


第二种方法 
        let array = [1, 2, 2,3,4,5,6,7,7,8,9];
          function de_Weight (array) {
             return new Set(array)    Set ES6 方法
        }
     let index = de_Weight(array)
    console.log(index);  //1 2 3 4 5 6 7 8 9


上一篇 下一篇

猜你喜欢

热点阅读