前端面試:你以為這只是一個簡單的數(shù)組去重嗎?
前言
之前面試過程被問到數(shù)組去重有哪些方式?心想這個問題我會,隨便也能說出好幾種,也不帶多思考的。巴拉巴拉巴拉巴拉。說完,面試官好像不太滿意的樣子,還問了句,沒了嗎。我想,咋滴,就這些還不不夠用嗎。然后就下一題了。
后來我看到這樣的數(shù)據(jù),忽然意識到,之前的面試怕不是草率了,完全沒考慮復雜數(shù)據(jù)結構,難怪他聽完不太滿意的樣子。
let arr = [1, 1, '2', 3, 1, 2,
{ name: '張三', id: { n: 1 }},
{ name: '張三', id: { n: 1 }},
{ name: '張三', id: { n: 2 }}
]
在平時的開發(fā)過程中這樣的數(shù)據(jù)應該少見,絕大部分的數(shù)組中數(shù)據(jù)格式應該保持一致,特殊情況就不管他了。
今天再來出一期數(shù)組去重的方式。
基本數(shù)據(jù)類型去重
確實有多種方式,比如set結構轉一下,lodash提供的uniq方法,或者自己利用其他方式實現(xiàn)也是很多的。今天就介紹幾個
當面試官詢問數(shù)組去重時,可以考慮到數(shù)組可能包含基本數(shù)據(jù)類型、引用數(shù)據(jù)類型,或者混合了兩種類型的情況。以下是針對不同情況的多種思路和實現(xiàn)方法:
使用 Set 數(shù)據(jù)結構(ES6)
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // [1, 2, 3, 4, 5]
使用 Array.prototype.filter()
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = array.filter((item, index) => array.indexOf(item) === index);
console.log(uniqueArray); // [1, 2, 3, 4, 5]
使用 Array.prototype.reduce()
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = array.reduce((acc, curr) => {
if (!acc.includes(curr)) {
acc.push(curr);
}
return acc;
}, []);
console.log(uniqueArray); // [1, 2, 3, 4, 5]
使用 for 循環(huán)和 Array.prototype.indexOf()
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = [];
for (let i = 0; i < array.length; i++) {
if (uniqueArray.indexOf(array[i]) === -1) {
uniqueArray.push(array[i]);
}
}
console.log(uniqueArray); // [1, 2, 3, 4, 5]
Lodash 是一個非常流行的 JavaScript 工具庫,提供了許多實用的函數(shù)來簡化開發(fā)過程。它也包含了一些用于數(shù)組去重的方法。以下是幾種利用 Lodash 實現(xiàn)數(shù)組去重的方法:
使用 _.uniq() 方法
const _ = require('lodash');
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = _.uniq(array);
console.log(uniqueArray); // [1, 2, 3, 4, 5]
使用 _.uniqWith() 方法(使用自定義比較函數(shù))
const _ = require('lodash');
const array = [{id: 1}, {id: 2}, {id: 1}];
const uniqueArray = _.uniqWith(array, (a, b) => _.isEqual(a, b));
console.log(uniqueArray); // [{id: 1}, {id: 2}]
2. 數(shù)組包含引用數(shù)據(jù)類型:
使用 Set 數(shù)據(jù)結構,利用自定義比較函數(shù)(如果需要去重對象數(shù)組)
const array = [{id: 1}, {id: 2}, {id: 1}];
const uniqueArray = Array.from(new Set(array.map(JSON.stringify)), JSON.parse);
console.log(uniqueArray); // [{id: 1}, {id: 2}]
使用 Array.prototype.reduce()
const array = [{id: 1}, {id: 2}, {id: 1}];
const uniqueArray = array.reduce((acc, current) => {
const isExist = acc.some(item => JSON.stringify(item) === JSON.stringify(current));
if (!isExist) acc.push(current);
return acc;
}, []);
console.log(uniqueArray); // [{id: 1}, {id: 2}]
上面都是使用了JSON.stringfy。但有個問題,如果引用數(shù)據(jù)的順序不一樣,轉的string就不等了。所以還是自己實現(xiàn)一個方法好一些。
使用自定義比較函數(shù)
這種方法適用于任何類型的數(shù)組,包括混合了基本數(shù)據(jù)類型和引用數(shù)據(jù)類型的數(shù)組。
function uniqueArray(array) {
const seen = new Map();
return array.filter(item => {
if (typeof item === 'object' && item !== null) {
const key = Object.keys(item).sort().map(k => `${k}:${item[k]}`).join('|');
if (!seen.has(key)) {
seen.set(key, true);
return true;
}
} else {
if (!seen.has(item)) {
seen.set(item, true);
return true;
}
}
return false;
});
}
const array = [1, 2, {id: 1}, {id: 2}, 1, {id: 1}];
const uniqueArr = uniqueArray(array);
console.log(uniqueArr); // [1, 2, {id: 1}, {id: 2}]
這個方法利用了 Map 數(shù)據(jù)結構的特性,用鍵來存儲數(shù)組中的元素,保證了元素的唯一性。對于對象,通過將屬性名排序并拼接成字符串來判斷是否重復。
如果都是引用結構,我們平時也可以使用 _.uniqBy() 方法去重,比如根據(jù)id什么的
const _ = require('lodash');
const array = [{id: 1}, {id: 2}, {id: 1}];
const uniqueArray = _.uniqBy(array, JSON.stringify);
console.log(uniqueArray); // [{id: 1}, {id: 2}]