Home Web Front-end JS Tutorial Summary of js array deduplication methods_javascript skills

Summary of js array deduplication methods_javascript skills

May 16, 2016 pm 03:48 PM
Array deduplication

Three methods

Use indexOf to determine the new array

A similar indexOf
is actually used in underscore.js

 //传入数组
 function unique1(arr){
  var tmpArr = [];
  for(var i=0; i<arr.length; i++){
   //如果当前数组的第i已经保存进了临时数组,那么跳过,
   //否则把当前项push到临时数组里面
   if(tmpArr.indexOf(arr[i]) == -1){
    tmpArr.push(arr[i]);
   }
  }
  return tmpArr;
 }
Copy after login

Use indexOf to determine the old array

 function unique2(arr){
  var tmpArr = []; //结果数组
  for(var i=0; i<arr.length; i++){
   //如果当前数组的第i项在当前数组中第一次出现的位置不是i,
   //那么表示第i项是重复的,忽略掉。否则存入结果数组
   if(arr.indexOf(arr[i]) == i){
    tmpArr.push(arr[i]);
   }
  }
  return tmpArr;
 }

Copy after login

Use hash to search

The implementation of JS objects used here is the characteristics of the hash table

 function unique3(arr){
  var tmpArr = [], hash = {};//hash为hash表
  for(var i=0;i<arr.length;i++){
   if(!hash[arr[i]]){//如果hash表中没有当前项
    hash[arr[i]] = true;//存入hash表
    tmpArr.push(arr[i]);//存入临时数组
   }
  }
  return tmpArr;
 }
Copy after login

Array expansion

 Array.prototype.unique1 = function (){
  var tmpArr = []; 
  for (var i = 0; i < this.length; i++){
   if (tmpArr.indexOf(this[i]) == -1){
    tmpArr.push(this[i]);
   }
  }
  return tmpArr;
 }

 Array.prototype.unique2 = function(){
   var tmpArr = []; //结果数组
   for(var i = 0; i < this.length; i++){
    if (this.indexOf(this[i]) == i){
     tmpArr.push(this[i]);
    }
   }
   return tmpArr;
 }

 Array.prototype.unique3 = function(){
   var tmpArr=[], hash = {};
   for(var i = 0; i < this.length; i++){
    if (!hash[this[i]]){
      hash[this[i]] = true; 
      tmpArr.push(this[i]); 
    }
   }
   return tmpArr;
 }

Copy after login

Use Set

Set and Map are new data structures in ES6
Set can directly store a non-duplicate set of keys. This key can also be an object, a string, etc.
Create set

var s = new Set([1, 2, 3,]);
s; // Set {1, 2, 3}

Copy after login

New element

>>> s.add(4)
>>> s
{1, 2, 3, 4}
>>> s.add(4)
>>> s
{1, 2, 3, 4}//重复元素不会被添加

Copy after login

Delete element

s; // Set {1, 2, 3, 4}
s.delete(3);
s; // Set {1, 2, 4}

Copy after login

Traverse elements

Map and Set cannot use subscripts
The ES6 standard introduces a new iterable type. Array, Map and Set all belong to iterable types

var s = new Set(['A', 'B', 'C']);

for (var x of s) { // 遍历Set
  alert(x);
}

Copy after login

Or directly use iterable’s built-in forEach method
The forEach method is introduced by the ES5.1 standard

var s = new Set(['A', 'B', 'C']);
s.forEach(function (element, set) {
  alert(element);
});
Copy after login

The above is the entire content of this article, I hope you all like it.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Specify the basis for removing duplicate elements when deduplicating PHP arrays Specify the basis for removing duplicate elements when deduplicating PHP arrays Apr 28, 2024 pm 10:48 PM

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 implement array deduplication in es5 and es6 How to implement array deduplication in es5 and es6 Jan 16, 2023 pm 05:09 PM

In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax "for(i=0;i<array length;i++){a=newArr.indexOf(arr[i]);if(a== -1){...}}". In es6, you can use the spread operator, Array.from() and Set to remove duplication; you need to first convert the array into a Set object to remove duplication, and then use the spread operator or the Array.from() function to convert the Set object back to an array. Just group.

How to maintain key-value correspondence after deduplication in PHP array? How to maintain key-value correspondence after deduplication in PHP array? Apr 27, 2024 pm 12:33 PM

Methods to maintain key-value correspondence after PHP array deduplication include: use the array_unique() function to remove duplicate values, and then use the array_flip() function to exchange key-value pairs. Merge the original array with the deduplicated array, and use the array merging method to retain key-value correspondence.

How to remove duplicates from an array based on specific key-value pairs in PHP? How to remove duplicates from an array based on specific key-value pairs in PHP? Apr 28, 2024 pm 06:18 PM

In PHP, use the array_unique() function to remove duplicates from an array based on specific key-value pairs. When calling the function, pass in the array as a parameter and select the sorting method as the second parameter. This function returns a new array in which duplicates have been removed based on the specified key-value pairs.

Tips for handling empty values ​​and null values ​​when deduplicating PHP arrays Tips for handling empty values ​​and null values ​​when deduplicating PHP arrays Apr 26, 2024 pm 05:03 PM

Tips for handling empty and null values ​​when deduplicating PHP arrays: Use array_unique with array_filter to filter out empty and null values. Use array_unique and define a custom comparison function to treat empty and null values ​​as equal. Use array_reduce to iterate over an array and add items if they do not contain empty or null values.

How to exclude duplicate arrays in PHP How to exclude duplicate arrays in PHP Jun 05, 2023 pm 02:53 PM

Methods to exclude duplicate arrays in PHP: 1. Create a PHP sample file; 2. Define the array to be deduplicated as "$oldArr" and the new array after deduplication as "$newArr"; 3. Use "array_unique()" The function removes duplicate elements from the array and returns the deduplicated array. The code is "$newArr = array_unique($oldArr);" to eliminate duplicate elements. 4. Deduplication can also be performed through a for loop.

How to remove duplicate elements from an array in php How to remove duplicate elements from an array in php May 25, 2023 pm 05:19 PM

How to remove duplicate elements in an array in PHP: 1. Use the "array_unique()" function to remove duplicate data in the array; 2. Traverse through a foreach loop and define a new array to store non-duplicate data to achieve deduplication; 3. Use the array_flip() and array_keys() functions to get the deduplicated array; 4. Use the array_filter() function to deduplicate the original array by using this function combined with an anonymous function.

How to remove duplicates from a javascript array How to remove duplicates from a javascript array Sep 07, 2021 pm 05:59 PM

Method: 1. Use the "[...new Set(arr)]" statement; 2. Use the "Array.from(new Set(arr))" statement; 3. Use the filter and indexOf functions; 4. Use double for loops , check whether the values ​​are duplicated, and use push() to delete them if there are duplicates.

See all articles