How to remove duplicates from commonly used arrays in JavaScript
Today, I would like to share with you the relevant knowledge points of JavaScript commonly used arrays, which are detailed in content and clear in logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
1. Make use of the properties of the object
Use the property that the object property does not have the same name.
Var arr = ["qiang", "ming", "tao", "li", "liang", "you", "qiang", "tao"]; console.time ("nonredundant1"); var nonredundant1 = Object.getOwnPropertyNames (function (seed, item, index) {seed [item] = index; return seed;}, {}); console.timeEnd ("nonredundant1"); console.log (nonredundant1)
The results are as follows:
two。 Using Set data structures
Set is an array-like structure, but there are no duplicate values in the set member. The set () function can take an array or an array of classes to generate a set object. The Array.from method is used to turn two types of objects into real arrays: array-like objects (array-like object and traverable iterable), including ES6's new data structures Set and Map).
Var arr = ["qiang", "ming", "tao", "li", "liang", "you", "qiang", "tao"]; function unique (arr) {return Array.from (new Set (arr))} console.time ("nonredundant2"); var nonredundant2 = unique (arr); console.timeEnd ("nonredundant2"); console.log (nonredundant2)
The results are as follows:
3. Using for loops and splice
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]) { //第一个等同于第二个,splice方法删除第二个 arr.splice(j, 1); j--; } } } return arr;}console.time("nonredundant3");var arr = ["qiang", "ming", "tao", "li", "liang", "you", "qiang", "tao"];var nonredundant3 = unique(arr);console.timeEnd("nonredundant3");console.log(nonredundant3); 结果如下: 4.使用indexOf判断去重 function unique(arr) { var array = []; for (var i = 0; i < arr.length; i++) { if (array .indexOf(arr[i]) === -1) { array .push(arr[i]) } } return array;}var arr = ["qiang", "ming", "tao", "li", "liang", "you", "qiang", "tao"];console.time("nonredundant4");var nonredundant4 = unique(arr);console.timeEnd("nonredundant4");console.log(nonredundant4); 结果如下: 5.使用sort排序去重 function unique(arr) { 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 = ["qiang", "ming", "tao", "li", "liang", "you", "qiang", "tao"];console.time("nonredundant5");var nonredundant5 = unique(arr);console.timeEnd("nonredundant5"); 结果如下: 6.使用filter 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 = ["qiang", "ming", "tao", "li", "liang", "you", "qiang", "tao"];console.time("nonredundant6");var nonredundant6 = unique(arr);console.timeEnd("nonredundant6");console.log(nonredundant6); 结果如下: 7.使用Map数据结构去重 function unique(arr) { let map = new Map(); let array = new Array(); // 数组用于返回结果 for (let i = 0; i < arr.length; i++) { if (map.has(arr[i])) { // 如果有该key值 map.set(arr[i], true); } else { map.set(arr[i], false); // 如果没有该key值 array.push(arr[i]); } } return array;} var arr = ["qiang", "ming", "tao", "li", "liang", "you", "qiang", "tao"];console.time("nonredundant7");var nonredundant7 = unique(arr);console.timeEnd("nonredundant7");console.log(nonredundant7); 结果如下: 8.使用reduce和include去重 function unique(arr){ return arr.reduce((prev,cur) =>Prev.includes (cur)? Prev: [... prev,cur], []);} var arr = ["qiang", "ming", "tao", "li", "liang", "you", "qiang", "tao"]; console.time ("nonredundant8"); var nonredundant8 = unique (arr); console.timeEnd ("nonredundant8"); console.log (nonredundant8)
The results are as follows:
These are all the contents of the article "how to duplicate JavaScript arrays". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.