Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report