Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods of removing duplicates in six arrays of js?

Shulou Source: shulou.com Published: 2022-06-01 05:54:39 10月12日 Update

This article mainly introduces the relevant knowledge of what js six array deduplication methods are, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this js six array deduplication methods. Let's take a look at it.

The first new Set

Var fun = function (arr) {

Return [... New Set (arr)]

}

The second kind of new Map

Var fun = function (arr) {

Let map = new Map ()

Let res = []

For (let I = 0; I

< arr.length; i++) { if (!map.has(arr[i])) { map.set(arr[i], i); res.push(arr[i]); } } return arr; }; 第三种 两层for循环+splice var fun = function (arr) { let len = arr.length; let res = []; for (let i = 0; i < len; i++) { for (let j = i + 1; j < len; j++) { if (arr[i] === arr[j]) { arr.splice(j, 1); len--; j--; i--; } } } return arr; }; 第四种 indexOf var fun = function (arr) { let res = []; for (let i = 0; i < arr.length; i++) { if(res.indexOf(arr[i])==-1) { res.push(arr[i]) } } return res }; 第五种 includes var fun = function (arr) { let res = []; for (let i = 0; i < arr.length; i++) { if (!res.includes(arr[i])) { res.push(arr[i]) } } return res }; 第六种 filter var fun = function (arr) { return arr.filter((item, index) =>

{

Return arr.indexOf (item) = = index

})

}

This is the end of the article on "what are the methods of removing duplicates in js six arrays?" Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the methods of removing weight in js six arrays". If you still want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Array method knowledge content article value easy to operate article easy to understand more watch industry information information channels channels jackers learning loops Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL OPPO Reno Xiaomi Linux MariaDB