* Object array decrement
1. reduce()
Var arrList = [{key: 1, value: "aaa"}, {key: 2, value: "BBB "}, {key: 3, value: "ccc"}, {key: 4, value: "aaa"}, {key: 5, value: "aaa"}, {key: 6, value: "ddd"}, ]; Let obj = {}; let obj = {}; let obj = {}; arrList = arrList.reduce((cur,next) => { obj[next.value] ? "" : obj[next.value] = true && cur.push(next); return cur; } []); Console. log(arrList);Copy the code
The –reduce() method performs a reducer function (in ascending order) that you provide on each element in the array, summarizing its results into a single return value.
Syntax Array. reduce(function(Total, currentValue, currentIndex, ARR), initialValue) parameter Description Total Required. The initial value, or the return value at the end of the calculation. CurrentValue required. The current element currentIndex is optional. The index ARR of the current element is optional. The array object to which the current element belongs. The initialValue is optional. Array. reduce(function(total, currentValue, index, arr){// total: cumulative variable, default is the first member of the array // currentValue: The current variable, which defaults to the second member of the array // index: current position (starting from 0) // arr: (1, 2, 3, 4, 5). Reduce (function (a, b) {return a + b; }) // 1 2/3 3 // 6 4 // 10 5 // Final result: 15Copy the code
* Plain array decrement
1. Use Es6 set to remove weight
Function unique (arr) {return Array. The from (new Set (arr)]} var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}]; console.log(unique(arr)) //[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {}, {}]Copy the code
2. Use for to nest for, and use splice to deduplicate
function unique(arr){ for(var i=0; i<arr.length; i++){ for(var j=i+1; j<arr.length; J ++){if(arr[I]==arr[j]){// if(arr[I]==arr[j]); j--; } } } return arr; } var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,' NaN ', 0, 0, 'a', 'a',{},{}]; Console. log(unique(arr)) //[1, "true", 15, false, undefined, NaN, NaN, "NaN", "a", {...}, {...}] //NaN and {} are nullCopy the code
3. Use indexOf to remove weight
function unique(arr) { if (! Array.isArray(arr)) { console.log('type error! ') return } var array = []; for (var i = 0; i < arr.length; i++) { if (array .indexOf(arr[i]) === -1) { array .push(arr[i]) } } return array; } var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,' NaN ', 0, 0, 'a', 'a',{},{}]; Console. log(arr) // [1, "true", true, 15, false, undefined, null, NaN, NaN, "NaN", 0, "a", {...}, {...}] //NaN, {} are not de-weightedCopy the code
4. Use the sort ()
function unique(arr) { if (! Array.isArray(arr)) { console.log('type error! ') return; } arr = arr.sort() var arrry= [arr[0]]; for (var i = 1; i < arr.length; i++) { if (arr[i] ! == arr[i-1]) { arrry.push(arr[i]); } } return arrry; } var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,' NaN ', 0, 0, 'a', 'a',{},{}]; The console. The log (unique (arr)) / / [0, 1, 15, "NaN," NaN, NaN, {...}, {...}, "a", false, null, true, "true", Undefined] //NaN, {Copy the code
5. Use the property of the object is not the same characteristics for deduplication (this array deduplication method has problems, not recommended, needs to be improved)
function unique(arr) { if (! Array.isArray(arr)) { console.log('type error! ') return } var arrry= []; var obj = {}; for (var i = 0; i < arr.length; i++) { if (! obj[arr[i]]) { arrry.push(arr[i]) obj[arr[i]] = 1 } else { obj[arr[i]]++ } } return arrry; } var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,' NaN ', 0, 0, 'a', 'a',{},{}]; Console. log(unique(arr)) //[1, "true", 15, false, undefined, null, NaN, 0, "a", {...}Copy the code
6. Use includes
function unique(arr) { if (! Array.isArray(arr)) { console.log('type error! ') return } var array =[]; for(var i = 0; i < arr.length; i++) { if( ! Array.push (arr[I]); array.push(arr[I]); }} return array} var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}]; The console. The log (unique (arr)) / / [1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}] / / {} not to heavyCopy the code
7. Using hasOwnProperty
function unique(arr) { var obj = {}; return arr.filter(function(item, index, arr){ return obj.hasOwnProperty(typeof item + item) ? false : (obj [typeof item + item] = true)})} var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined. null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}]; Console. log(unique(arr)) //[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {...}Copy the code
8. Use the filter
Arr. Filter (function(item, index, arr) {return arr. Filter (function(item, index, arr) {return arr. Return arr.indexof (item, 0) === index; }); } var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,' NaN ', 0, 0, 'a', 'a',{},{}]; The console. The log (unique (arr)) / / [1, "true", true, 15, false, undefined, null, "NaN," 0, "a", {...}, {...}]Copy the code
9. Use recursion to duplicate
function unique(arr) {
var array= arr;
var len = array.length;
array.sort(function(a,b){ //排序后更加方便去重
return a - b;
})
function loop(index){
if(index >= 1){
if(array[index] === array[index-1]){
array.splice(index,1);
}
loop(index - 1); //递归loop,然后数组去重
}
}
loop(len-1);
return array;
}
var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
//[1, "a", "true", true, 15, false, 1, {…}, null, NaN, NaN, "NaN", 0, "a", {…}, undefined]
Copy the code
10. Use Map data structure for deduplication
function arrayNonRepeatfy(arr) { let map = new Map(); let array = new Array(); For (let I = 0; i < arr.length; I++) {if (map) from the (arr) [I]) {/ / if you have the key value map. The set (arr [I], true); } else { map .set(arr[i], false); Array.push (arr[I]); } } return array ; } var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,' NaN ', 0, 0, 'a', 'a',{},{}]; The console. The log (unique (arr)) / / [1, "a", "true", true, 15, false, 1, {...}, null, NaN, NaN, "NaN," 0, "a", {...}, undefined]Copy the code
11. Use the reduce + includes
function unique(arr){ return arr.reduce((prev,cur) => prev.includes(cur) ? prev : [...prev,cur],[]); } var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,' NaN ', 0, 0, 'a', 'a',{},{}]; console.log(unique(arr)); / / (1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]Copy the code
12.[…new Set(arr)]
[...new Set(arr)] ----Copy the code
——————————————
* Array of various methods
1. Concat — Merge arrays
Const arr1 = [1,2,3] const arr2 = [4,5,6] const arr3 = arr1.concat(arr2)console.log(arr3) // [1,2,3,4,5,6]Copy the code
2. Sort — array sort
Sort () changes the array to sort by Unicode code
-
Object array sort
Var arr = [{name: “xiao Ming,” age: 12}, {name: “little red”, the age: 11}, {name: “xiao”, the age: 15}, {name: “wah”, the age: 13}].
Return function(m,n){var a = m[p]; return function(m,n){var a = m[p]; var b = n[p]; return a – b; }} arr.sort(compare(“age”)); console.log(arr); / / the result is as follows: / / [{name: “little red”, the age: 11}, / / {name: “xiao Ming,” age: 12}, / / {name: “wah”, the age: 13}, / / {name: “xiao”, the age: 15}]
-
Sort an array
Not using the comparison function results in the following situation, which is not the desired result
Var arr =,3,13,17,4,19,1 [2]; Arr.sort () // result: [1, 13, 17, 19, 2, 3, 4]Copy the code
If you want to sort an array by size, you add a comparison function to the sort() method
Var arr =,3,13,17,4,19,1 [2]; Arr. Sort (function(a,b){return b - a; / / descending}) console. The log (arr) / / results: [17, 19, 13, 4, 3, 2, 1]Copy the code
3. FindIndex — The array returns the index if it meets the criteria
Index 0/1/2/3/4 does not meet the requirement to return “-1”
Const arr = [1,2,3,4,5,6,7,8] Let res = arr.findindex (item => item >=7) console.log(res) // 6 Let res2 = arr.findIndex(item =>10) console.log(res) // -1Copy the code
4. Join — Converts an array to a string and returns it
If the array has only one element, the delimiter is not returned
Const arr = [1,2,3,4] const arr1 = [null,1,undefined,2] console.log(arr.join()) // '1,2,3,4'console.log(arr.join(")) // '1234' in the console. The log (arr. Join (' - ') / / 'the 1-2-3-4' console. The log (arr1. Join (' ')) / / '11'Copy the code