自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

前端面試:你以為這只是一個簡單的數(shù)組去重嗎?

開發(fā) 前端
確實有多種方式,比如set結構轉一下,lodash提供的uniq方法,或者自己利用其他方式實現(xiàn)也是很多的。今天就介紹幾個。

前言

之前面試過程被問到數(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}]
責任編輯:武曉燕 來源: 海燕技術棧
相關推薦

2016-09-09 08:27:16

2018-08-16 21:40:05

面試簡歷技術

2019-03-29 08:46:46

4G5G網(wǎng)速

2024-11-28 10:09:06

2020-03-13 13:45:41

前端面試Web

2022-08-19 10:27:39

系統(tǒng)模型

2020-04-03 09:35:33

前端框架Vue

2013-06-05 10:32:38

GoogleAndroid Stu

2021-06-08 07:48:26

iOS 15 Linux 操作系統(tǒng)

2019-08-22 17:19:19

javascript去重數(shù)組

2024-01-15 00:35:23

JavaScript框架HTML

2017-09-06 09:13:24

2023-06-26 08:24:23

JavaScriptAJAX

2021-12-31 16:16:04

JavaScript數(shù)組代碼

2017-08-16 10:03:57

前端面試題算法

2016-10-17 10:15:45

俞永福高德日活百度

2020-11-06 09:05:18

前端web開發(fā)

2023-07-11 13:34:19

Rust開發(fā)軟件

2024-05-28 10:14:31

JavaScrip模板引擎

2021-07-07 06:16:29

EmacsMeta鍵編程
點贊
收藏

51CTO技術棧公眾號