Six methods of array de duplication have also come!

The array object de duplication is completed. Let's look at the array de duplication.

1, The first method

  1. Use indexOf() and push() for array de duplication.

The indexOf() method returns the first occurrence of a specified string value in the string.
If no matching string is found, - 1 is returned.
Note: the indexOf() method is case sensitive.

	var arr = [3,3,undefined,undefined,null,null,NaN,NaN,{},{},true,true,'false','false'];

	function unique(arr) {
		if (!Array.isArray(arr)) { //Determine whether it is an array
				console.log('Not an array!')
				return;
		}
		var newArr = [];
		for (var i = 0; i < arr.length; i++) {
			if (newArr.indexOf(arr[i]) === -1) {
				newArr.push(arr[i]);
			}
		}
		return newArr;
	}
	var demo = unique(arr);
	console.log("De duplicated array", demo);
	//De duplicated array (9) [3, undefined, null, NaN, NaN, {...}, {...}, true, "false"]
  • NaN and {} were not de duplicated

2, The second method

  1. Use sort () and push () for array de duplication.

The sort() method is used to sort the elements of the array (ascending and descending).
sort() the default sort order is ascending alphabetically.
Note: default alphabetic sorting: 40 (four) is before 5 (five).

	var arr = [3, 3, undefined, undefined, null, null, NaN, NaN, {}, {}, true, true, 'false', 'false'];

	function unique(arr) {
		arr = arr.sort();
		//Sorted array (14) [3, 3, NaN, NaN, {...}, {...}, "false", "false", null, null, true, true, undefined, undefined]
		
		var arrry = [arr[0]];
		for (var i = 1; i < arr.length; i++) {
			if (arr[i] !== arr[i - 1]) {
				arrry.push(arr[i]);
			}
		}
		return arrry;
	}
	console.log("De duplicated array", (unique(arr)));
	//De duplicated array (9) [3, NaN, NaN, {...}, {...}, "false", null, true, undefined]
  • NaN and {} were not de duplicated

3, The third method

  1. Use includes() for array de duplication.

The includes() method is used to determine whether the string contains the specified substring.
Returns true if a matching string is found, otherwise false.
The includes() method is case sensitive.
Syntax: str.includes (searchValue, start)
searchValue: the string to find. start: the starting index position (not writable).

	var arr = [3, 3, undefined, undefined, null, null, NaN, NaN, {}, {}, true, true, 'false', 'false'];
	function unique(arr) {
		var array = [];
		for (var i = 0; i < arr.length; i++) {
			if (!array.includes(arr[i])) { //includes detects whether the array has a value
				array.push(arr[i]);
			}
		}
		return array
	}
	console.log("De duplicated array", (unique(arr)));
	//De duplicated array (8) [3, undefined, null, NaN, {...}, {...}, true, "false"]
  • {} was not removed

4, The fourth method

  1. Use filter () for array de duplication.

As the name suggests, filter.
The filter() method creates a new array. The elements in the new array are checked by checking all the qualified elements in the specified array.
filter() does not detect empty arrays. The original array will not be changed.
array.filter(function(currentValue,index,arr), thisValue)

currentValueCurrent element value (required)
indexCurrent element index value (adjustable)
arrArray object to which the current element belongs (optional)
thisValueObject is used when the callback is executed and passed to the function as the value of "this". (optional)
		var arr = [3, 3, undefined, undefined, null, null, NaN, NaN, {}, {}, true, true, 'false', 'false'];
			function unique(arr) {
				return arr.filter(function(currentValue,index,arr){
					return arr.indexOf(currentValue, 0) === index;
				});
			}
			console.log("De duplicated array", (unique(arr)));
		//De duplicated array (7) [3, undefined, null, {...}, {...}, true, "false"]
  • {} not removed; NaN was filtered and disappeared

5, The fifth method

  1. Use Set() of ES6 for array de duplication.

New Set: array to Set.
The value stored in the Set object is always unique, so you need to judge whether the two values are identical.
Array.from(object, mapFunction, thisValue)
Object: the object to convert to an array. (required)

			var arr = [3, 3, undefined, undefined, null, null, NaN, NaN, {}, {}, true, true, 'false', 'false'];
			//First put in Set to make the value unique, and then convert it back to array format
			function unique (arr) {
			  return Array.from(new Set(arr))
			}
			console.log("De duplicated array", (unique(arr)));
			//De duplicated array (8) [3, undefined, null, NaN, {...}, {...}, true, "false"]
  • {} was not removed

6, Summary

These are mainly the methods I am familiar with using javascript.
Reference directions: javaScript array de duplication of 12 methods in big brother version
Reference directions: JavaScript rookie tutorial

Keywords: Javascript Front-end array

Added by slashpine on Sat, 25 Dec 2021 03:49:40 +0200