How to use deduplication operation in javascript
This article mainly explains "how to use de-reoperation in javascript". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use reoperation in javascript.
Core basic algorithm
Iterate through the obtained data array, and then store the elements in the array into a new array. Check whether the elements of the array already exist before storing them in the new array. If they exist (the same element, that is, repeating elements), they will no longer be placed in the new array.
The basic usage of data deduplication
In JS, the use of deduplication operation is generally about array deduplication, string deduplication, number deduplication. The specific examples are shown below.
1. Array deduplication
There are three ways to remove duplicates from arrays:
Method 1: indexOf cycle weight removal
Method 2: Set weight removal of ES6; Array.from (new Set (arr))
Method 3: remove the weight of the Object key value pair; store the value of the array as the key value of Object, such as Object [valuea] = true. When judging another value, if Object [valueb] exists, it means that the value is repeated.
Here we only use the indexOf loop to do an example.
Sample array:
Var array = [1, 2, 3, 4, 5, 5, 4, 3, 2]
Treatment method:
Function repeat (array) {var array1 = []; / / create a new array to store the value for of array (var item0)