var arr = [1, 6, 6, 6, 3, 9, 4, 9, 3, 8, 2, 2]
var newArr = []
console.log(arr, '原数组');
var flag
for (let i = 0; i < arr.length; i++) {
flag = true
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) {
flag = false
}
}
if (flag) newArr.push(arr[i])
}
console.log(newArr);
var arr = [1, 6, 3, 9, 4, 9, 3, 8, 2]
function getUniqueArray2() {
var result = Array.from(new Set(arr))
console.log(result);
}
getUniqueArray2()
var arr = [1, 6, 3, 9, 4, 9, 3, 8, 2]
function getUniqueArray3() {
var result = []
arr.forEach((item) => {
if (result.indexOf(item) === -1) result.push(item)
})
console.log(result);
}
getUniqueArray3()
var arr = [1, 6, 3, 9, 4, 9, 3, 8, 2]
function getUniqueArray4() {
var result = arr.filter((item, index) => {
return arr.indexOf(item) === index
})
console.log(result);
}
getUniqueArray4()
var arr = [1, 6, 3, 9, 4, 9, 3, 8, 2]
function getUniqueArray5() {
var result = arr.reduce((pre, item) => {
return pre.includes(item) ? pre : [...pre, item]
}, [])
console.log(result);
}
getUniqueArray5()
var arr = [1, 6, 3, 9, 4, 9, 3, 8, 2]
function getUniqueArray6() {
var result = {}
arr.forEach((item, index) => {
result[arr[index]] = item
});
result = Object.keys(result).map(item => item - 0)
console.log(result);
}
getUniqueArray6()
|