// 数组去重
const arr = ["1", "1", "2", "3", "5", "3", "1", "5", "4"]
console.log(this.deduplicate(arr)) // ['1', '2', '3', '5', '4']
// 数组对象去重
const arr = [
{ id: 1, name: "数据1" },
{ id: 1, name: "数据2" },
{ id: 2, name: "数据3" },
{ id: 3, name: "数据4" },
{ id: 4, name: "数据5" },
{ id: 3, name: "数据6" },
{ id: 5, name: "数据7" }
]
console.log(this.deduplicate(arr, "id")) // [{ id: 1, name: "数据2" }, { id: 2, name: "数据3" }, { id: 3, name: "数据4" }, { id: 4, name: "数据5" }, { id: 5, name: "数据7" }]
方法一
| |
| |
| |
| |
| |
| |
| deduplicate(arr, t = "") { |
| const newArr = [arr[0]] |
| |
| if (t) { |
| for (let i = 1; i < arr.length; i++) { |
| let repeat = false |
| for (let j = 0; j < newArr.length; j++) { |
| if (t && arr[i][t] === newArr[j][t]) { |
| repeat = true |
| break |
| } |
| } |
| if (!repeat) newArr.push(arr[i]) |
| } |
| } |
| |
| else { |
| for (let i = 1; i < arr.length; i++) { |
| let repeat = false |
| for (let j = 0; j < newArr.length; j++) { |
| if (arr[i] === newArr[j]) { |
| repeat = true |
| break |
| } |
| } |
| if (!repeat) newArr.push(arr[i]) |
| } |
| } |
| return newArr |
| } |
复制
方法二
| |
| |
| |
| |
| |
| |
| deduplicate(arr, t = "") { |
| const sortArr = arr.sort(function (a, b) { |
| if (t) return a[t] - b[t] |
| return a - b |
| }), |
| newArr = [sortArr[0]] |
| |
| if (t) { |
| for (let i = 1; i < sortArr.length; i++) { |
| if (sortArr[i][t] !== sortArr[i - 1][t]) newArr.push(sortArr[i]) |
| } |
| } |
| |
| else { |
| for (let i = 1; i < sortArr.length; i++) { |
| if (sortArr[i] !== sortArr[i - 1]) newArr.push(sortArr[i]) |
| } |
| } |
| return newArr |
| } |
复制
方法三
| |
| |
| |
| |
| |
| |
| deduplicate(arr, t = "") { |
| const obj = {}, |
| newArr = [] |
| |
| if (t) { |
| for (let i = 0; i < arr.length; i++) { |
| if (!obj[arr[i][t]]) { |
| obj[arr[i][t]] = 1 |
| newArr.push(arr[i]) |
| } |
| } |
| } |
| |
| else { |
| for (let i = 0; i < arr.length; i++) { |
| if (!obj[arr[i]]) { |
| obj[arr[i]] = 1 |
| newArr.push(arr[i]) |
| } |
| } |
| } |
| return newArr |
| } |
复制
方法四
| |
| |
| |
| |
| |
| |
| deduplicate(arr, t = "") { |
| const newArr = [], |
| assignList = [] |
| |
| if (t) { |
| for (let i = 0; i < arr.length; i++) { |
| if (assignList.indexOf(arr[i][t]) === -1) { |
| assignList.push(arr[i][t]) |
| newArr.push(arr[i]) |
| } |
| } |
| } |
| |
| else { |
| for (let i = 0; i < arr.length; i++) { |
| if (newArr.indexOf(arr[i]) === -1) newArr.push(arr[i]) |
| } |
| } |
| return newArr |
| } |
复制
方法五
| |
| |
| |
| |
| |
| |
| deduplicate(arr, t = "") { |
| const newArr = [], |
| assignList = [] |
| |
| if (t) { |
| for (let i = 0; i < arr.length; i++) { |
| if (!assignList.includes(arr[i][t])) { |
| assignList.push(arr[i][t]) |
| newArr.push(arr[i]) |
| } |
| } |
| } |
| |
| else { |
| for (let i = 0; i < arr.length; i++) { |
| if (!newArr.includes(arr[i])) newArr.push(arr[i]) |
| } |
| } |
| return newArr |
| } |
复制
方法六
| |
| |
| |
| |
| |
| |
| deduplicate(arr, t = "") { |
| let newArr = [] |
| |
| if (t) { |
| newArr = arr.filter(function (item) { |
| return newArr.includes(item[t]) ? "" : newArr.push(item[t]) |
| }) |
| } |
| |
| else { |
| newArr = arr.filter(function (item) { |
| return newArr.includes(item) ? "" : newArr.push(item) |
| }) |
| } |
| return newArr |
| } |
复制
方法七
| |
| |
| |
| |
| |
| |
| deduplicate(arr, t = "") { |
| const newArr = [], |
| assignList = [] |
| |
| if (t) { |
| arr.forEach(item => { |
| if (!assignList.includes(item[t])) { |
| assignList.push(item[t]) |
| newArr.push(item) |
| } |
| }) |
| } |
| |
| else { |
| arr.forEach(item => { |
| return newArr.includes(item) ? "" : newArr.push(item) |
| }) |
| } |
| return newArr |
| } |
复制
方法八
| /** 数组去重 |
| * 思路:利用数组原型对象上的 splice 方法 |
| * @param arr 需要去重的数组 |
| * @param t 根据 t 字段(指定)进行去重 |
| * @returns {*[]} 已去重后的数据 |
| */ |
| deduplicate(arr, t = "") { |
| let i, |
| j, |
| len = arr.length |
| // 有指定字段 |
| if (t) { |
| for (i = 0; i < len; i++) { |
| for (j = i + 1; j < len; j++) { |
| if (arr[i][t] === arr[j][t]) { |
| arr.splice(j, 1) |
| len |
| j |
| } |
| } |
| } |
| } |
| // 无指定字段 |
| else { |
| for (i = 0; i < len; i++) { |
| for (j = i + 1; j < len; j++) { |
| if (arr[i] === arr[j]) { |
| arr.splice(j, 1) |
| len |
| j |
| } |
| } |
| } |
| } |
| return arr |
| } |
复制
方法九
| |
| |
| |
| |
| |
| |
| deduplicate(arr, t = "") { |
| const newArr = [], |
| assignList = [] |
| |
| if (t) { |
| for (let i = 0; i < arr.length; i++) { |
| if (assignList.lastIndexOf(arr[i][t]) === -1) { |
| assignList.push(arr[i][t]) |
| newArr.push(arr[i]) |
| } |
| } |
| } |
| |
| else { |
| for (let i = 0; i < arr.length; i++) { |
| newArr.lastIndexOf(arr[i]) !== -1 ? "" : newArr.push(arr[i]) |
| } |
| } |
| return newArr |
| } |
复制