Home >Web Front-end >JS Tutorial >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!