JavaScript Topic 2: Array Deduplication
Directory
- 1. Double-layer loop (violent method)
- 2. indexOf and includes
- 3. Sorting and deduplication
- 4. Filter
- 5. Key-value pair(key-value)
- 6.ES6
- 7 , some questions
- Reference
Write at the end
(Related free learning recommendations: javascript video tutorial)
1. Double-layer circulation
const unique = (arr)=>{ for(let i = 0; i { var arr = [1, '1', '1', 1, 2, true, false, true, 3, 2, 2, 1]; var newArr = []; for(let i = 0; i <p><strong>Core point:</strong></p>
- Time complexity:
O(n^2)
- The above two methods are two loop traversals, and the processing methods are slightly different
- The above implementation methods are indeed It’s not the best choice, but it has good compatibility~
2. indexOf and includes
2.1 indexOf simplifies one-level loop judgment
Core point:
- If you need to return the original array, you can find the duplicate item in the
indexOf
method (not equal to the position where it first appeared ) when usingsplice
to remove -
indexOf
: Returns thefirst index
where a given element can be found in the array, if not If exists, -1 is returned. -
indexOf(ele, fromIndex)
- ele: The element to be found
- fromIndex: The starting position of the element to be found, the default is 0, negative numbers are allowed, -2 means starting from the second to last element
- Return a subscript (number)
##Code:
const unique = (arr) => { var res = []; for (let i = 0; i 2.2 includesSimplify one layer of loop judgment<h5></h5><p>Core point:<strong></strong></p>
- You can combine it by yourself whether you want to return the original array or a new array~
- includes
: Used Determine whether an array contains a specified value. Depending on the situation, if it does, it will return
true, otherwise it will return
false ##includes(ele, fromIndex) -
ele: The element to be found
- fromIndex: Start searching at the specified index. The default is 0. If it is a negative value, jump forward by the absolute value of fromIndex
- indexes from the end. .
Return result (bool)
const unique = (arr) => {
var res = [];
for (let i = 0; i
Here we recommend using includes to find elements:
The return value can be directly used as the conditional statement of if, conciseif(res.indexOf(arr[i]) !== -1 ){ todo }// orif(res.includes(arr[i])){ todo }
NaNIf there is
in the array, and you just need to determine whether the array exists NaN
, then you use indexOf
cannot be judged, you must use the includes
method. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">var arr = [NaN, NaN];arr.indexOf(NaN); // -1arr.includes(NaN); // true</pre><div class="contentsignin">Copy after login</div></div>
undefinedIf there is an
value in the array, includes
will think The empty value is undefined
, but indexOf will not. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">var arr = new Array(3);console.log(arr.indexOf(undefined)); //-1console.log(arr.includes(undefined)) //true</pre><div class="contentsignin">Copy after login</div></div>
Core point:
After the array is sorted, the same elements will Adjacent, so if the current element is different from its adjacent elements, it is stored in a new array;
- Compared with indexOf, only one loop is needed;
- concat will splice the arrays , and returns a new array;
- sort() sorting is done by sorting according to the Unicode position of each character of the converted
- string. So it is difficult to guarantee its accuracy;
var arr = [1, 1, '1'];function unique(arr) {
var res = [];
var sortedArr = arr.concat().sort();
var last;
for (var i = 0; i
Core point:
filter: method creates a new array containing all elements of the test
- implemented by the provided
- function (returns the elements for which the test function is established)
- :
callback accepts three parameters: element-the element currently being processed, index-the current element index, array-the filter was called The array itself
- thisArg: The value used for this when executing callback. Using filter we can simplify the outer loop at the code level:
var arr = [1, 2, 1, 1, '1'];const unique = function (arr) {
var res = arr.filter(function(item, index, arr){
return arr.indexOf(item) === index;
})
return res;}console.log(unique(arr)); // [ 1, 2, '1' ]
var arr = [1, 2, 1, 1, '1'];const unique = function (arr) { return arr.concat().sort().filter(function(item, index, arr){ return !index || item !== arr[index - 1] })}console.log(unique(arr));
The methods mentioned above can be roughly divided into
Non-sorted array, two traversal judgments (traversal, query)- Sorted array, comparison of adjacent elements
- We propose another way, using the key-value of the Object object Method, to count the number of elements appearing in the array, there are two preliminary judgment logics
Take
[1,1,1,2,2,3,'3']for example: <ol>
<li>统计每个元素出现的次数,obj:{1: 3, 2: 2, 3: 3}, 返回这个<code>obj
的key
而不管他们的value
5.1 统计次数
var arr = [1, 2, 1, 1, '1', 3, 3];const unique = function(arr) { var obj = {}; var res = []; arr.forEach(item => { if (!obj[item]) { obj[item] = true; res.push(item); } }); return res;}console.log(unique(arr)); // [1, 2, 3]
5.2 结合filter
var arr = [1, 2, 1, 1, '1'];const unique = function(arr) { var obj = {}; return arr.filter(function(item, index, arr){ return obj.hasOwnProperty(item) ? false : (obj[item] = true) })}console.log(unique(arr)); // [1, 2]
5.3 key: value存在的问题
对象的属性是字符串类型的,即本身数字1
和字符串‘1’
是不同的,但保存到对象中时会发生隐式类型转换,导致去重存在一定的隐患。
考虑到string和number的区别(typeof 1 === ‘number’, typeof ‘1’ === ‘string’),
所以我们可以使用 typeof item + item
拼成字符串作为 key 值来避免这个问题:
var arr = [1, 2, 1, 1, '1', 3, 3, '2'];const unique = function(arr) { var obj = {}; var res = []; arr.forEach(item => { if (!obj[typeof item + item]) { obj[typeof item + item] = true; res.push(item); } }); return res;}console.log(unique(arr)); // [ 1, 2, '1', 3, '2' ]
六、ES6
随着 ES6 的到来,去重的方法又有了进展,比如我们可以使用 Set 和 Map 数据结构。
6.1 Set
Set:它允许你存储任何类型的唯一值,无论是原始值或者是对象引用
代码:
var arr = [1, 2, 1, '1', '2'];const unique = function(arr) { return Array.from(new Set(arr));}console.log(unique(arr)); // [ 1, 2, '1', '2' ]
简化1:
function unique(array) { return [...new Set(array)];}
简化2:
var unique = (a) => [...new Set(a)]
6.2 Map
Map 对象保存键值对,并且能够记住键的原始插入顺序。任何值(对象或者原始值) 都可以作为一个键或一个值。
- Map.prototype.has(key):返回一个布尔值,表示Map实例是否包含键对应的值。
- Map.prototype.set(key, value):设置Map对象中键的值。返回该Map对象。
function unique (arr) { const newMap = new Map() return arr.filter((a) => !newMap.has(a) && newMap.set(a, 1));}
写到这里比较常规的数组去重方法就总结的差不多了,如果需要更强大的去重方法,我们需要对他们进行组合,而且因为场景的不同,我们所实现的方法并不一定能涵盖到
相关免费学习推荐:javascript(视频)
The above is the detailed content of JavaScript Topic 2: Array Deduplication. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

PHP's array_unique() function is used to remove duplicate elements from an array. By default, strict equality (===) is used. We can specify the basis for deduplication through a custom comparison function: create a custom comparison function and specify the deduplication standard (for example, based on element length); pass the custom comparison function as the third parameter to the array_unique() function. Remove duplicate elements based on specified criteria.

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
