


What are the common methods of arrays and how to deduplicate arrays (code attached)
Arrays are an important part of JavaScript. Whether it is a job or a job interview, arrays will be involved, such as the classic question: How to remove duplicates from an array. Today I will talk to you about What are the common methods for arrays, and various ways to deduplicate arrays.
1. Common array methods
slice() is used to extract a part of the target array and return a new array, leaving the original array unchanged.
concat() is used to merge multiple arrays. It adds the members of the new array to the end of the members of the original array, and then returns a new array, leaving the original array unchanged.
reverse() is used to reverse the array elements and return the changed array. Note that this method will change the original array.
sort() Sorts the array members. The default is to sort the array members in dictionary order. After sorting, the original array will be changed.
push() is used to add one or more elements to the end of the array and returns the length of the array after adding the new elements. Note that this method will change the original array.
pop() is used to delete the last element of the array and return that element. Note that this method will change the original array.
unshift() is used to add an element to the first position of the array and returns the length of the array after adding the new element. Note that this method will change the original array.
shift() is used to delete the first element of the array and return the element. Note that this method will change the original array.
splice() is used to delete part of the original array members and add new array members at the deleted positions. The return value is the deleted element. Note that this method will change the original array.
map() Pass all members of the array into the parameter function in turn, and then return the results of each execution into a new array.
forEach() is very similar to the map method, and also executes the parameter function on all members of the array in sequence. However, the forEach method does not return a value and is only used to manipulate data.
filter() is used to filter array members, and the members that meet the conditions form a new array and return it.
join() uses the specified parameter as the delimiter to concatenate all array members into a string and return it. If no parameters are provided, they default to commas.
indexOf() Returns the position of the first occurrence of the given element in the array, or -1 if it does not appear.
lastIndexOf() Returns the position of the last occurrence of the given element in the array, or -1 if it does not appear.
2. Multiple ways to deduplicate arrays
1. Use objects
function unique(arr) { var uniqueArr = [], len = arr.length for (var i = 0; i < len; i++) { if (uniqueArr.indexOf(arr[i]) == -1) { uniqueArr.push(arr[i]) } } return uniqueArr } var arr = [1, 2, 3, 1, 5, "1"] var uniqueArr = unique(arr) console.log(arr) //[1, 2, 3, 1, 5, "1"] c onsole.log(uniqueArr) //[ 1, 2, 3, 5, '1' ]
2. Deduplicate after sorting
function unique(arr) { var uniqueArr = [], sortArr = arr.concat().sort(), len = sortArr.length, prev for (var i = 0; i < len; i++) { if (!i || prev !== sortArr[i]) { uniqueArr.push(sortArr[i]) } prev = sortArr[i] } return uniqueArr } var arr = [1, 2, 3, 1, 5, "1"] var uniqueArr = unique(arr) console.log(arr) //[ 1, 2, 3, 1, 5, '1' ] console.log(uniqueArr) //[ 1, '1', 2, 3, 5 ]
3. for loop indexOf
function unique(arr) { var uniqueArr = [], obj = {}, len = arr.length for (var i = 0; i < len; i++) { obj[typeof arr[i] + arr[i]] = arr[i] } for (var i in obj) { uniqueArr.push(obj[i]) } console.log(obj) //{ number1: 1, number2: 2, number3: 3, number5: 5, string1: '1' } return uniqueArr } var arr = [1, 2, 3, 1, 5, "1"] var uniqueArr = unique(arr) console.log(arr) // [1, 2, 3, 1, 5, "1"] console.log(uniqueArr) //[ 1, 2, 3, 5, '1' ]
4. es6 implementation method
function unique(arr) { return Array.from(new Set(arr)) } var arr = [1, 2, 3, 1, 5, "1"] var uniqueArr = unique(arr) console.log(arr) //[ 1, 2, 3, 1, 5, '1' ] console.log(uniqueArr) //[ 1, 2, 3, 5, '1' ]
The above is the detailed content of What are the common methods of arrays and how to deduplicate arrays (code attached). 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





The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

PHP is a commonly used server-side scripting language widely used in website development and data processing fields. In PHP, it is a very common requirement to sort the values in an array by size. By using the built-in sort function, you can easily sort arrays. The following will introduce how to use PHP to sort the values in an array by size, with specific code examples: 1. Sort the values in the array in ascending order:
