1.filter()數組
var arr = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 4, 5, 2, 3, 1, 2, 6, 1, 2, 3, 6, 7123, 213, 4, 5, 5, 6, 6, 14, 132, 123, ]
var array = arr.filter((item, value) => arr.indexOf(item) == value)
console.log(array)
複製代碼
2.sort()bash
var arr = [1, 2, 3, 4, 1, 2, 3, 4, 1]
var arr = arr.sort((a, b) => a - b);
//讓其按順序排列
console.log(arr)
var array = []
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== arr[i - 1]) {
array.push(arr[i])
}
}
console.log(array)
複製代碼
3.indexOf()spa
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 7, 7, 3, 4, 6, 834, 6, 12321, 123, 4, 5, 5, 6, 213]
var array = [];
for (i = 0; i < arr.length; i++) {
if (array.indexOf(arr[i]) === -1) {
array.push(arr[i])
}
console.log(array)
}
複製代碼
4.splice()code
var arr = [1, 1, 12, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 12, 3, 3, 4];
var i = void 0;
var j = void 0;
for (i = 0; i < arr.length; i++) {
for (j = i + 1; j < arr.length; j++) {
//從它key = 1的時候循環第二個數組
if (arr[i] == arr[j]) {
arr.splice(j, 1)
j = j - 1;
// 刪掉相應位置的數組以後應該依次往前提早一位
}
console.log(arr)
}
}
// 利用splice()進行數組去重
複製代碼