Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of removing duplicates of arrays by JS

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the method of JS to achieve array deduplication of what the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this JS method of array deduplication, let's take a look at it.

1.ES5 is commonly used: use for to nest for, and then splice to remove 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; } 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)) 解析:两个null消失,NaN和{}没有去重 2.ES6常用:Set去重 function unique(arr) { return Array.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))

Parsing: less reduplicated code. But the {} empty object cannot be removed.

3.indexOf deweighting function unique (arr) {if (! Array.isArray (arr)) {console.log ('type errorweights') Return} var array = []; / / create an empty array and for loop the original array to determine whether there is a current element in the array. If the same value is skipped, different push enter the 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(unique(arr)) 解析:NaN、{}没有去重 4.sort()排序//排序后的结果,遍历对比 function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } arr = arr.sort() var arrray = [arr[0]]; for (var i = 0; i < arr.length; i++) { if (arr[i] !== arr[i - 1]) { arrray.push(arr[i]); } } return arrray } 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)) 解析:NaN、{}没有去重 5.对象属性不能相同(不建议) function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } var array = []; var obj = {}; for (var i = 0; i < arr.length; i++) { if (!obj[arr[i]]) { array.push(arr[i]) obj[arr[i]] = 1 } else { obj[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(unique(arr)) 解析:两个true去掉,NaN和{}没有去重 6.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.includes(arr[i])) { //includes 检测数组是否有某个值 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(unique(arr)) 解析:{}没有去重 7.hasOwnPropertyfunction unique(arr) { var obj = {}; return arr.filter(function(item, index, arr){ return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true) })} 解析:所有都去重 8.filter function unique(arr) { return arr.filter(function (item, index, 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', {}, {} ]; console.log(unique(arr)) 解析:{}没有去重、NaN两个都没有了 9.利用递归去重 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); / / Recursive loop, then array deduplicated}} 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,' asides, 'asides, {}, {}]; console.log (unique (arr))

Parsing: NaN and {} are not deduplicated

10.Map deduplication function arrayNonRepeatfy (arr) {let map = new Map (); let array = new Array (); / / the array is used to return the result 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 = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN, 'NaN', 0, 0, 'a', 'a', {}, {} ]; console.log(arrayNonRepeatfy(arr)) 解析:创建一个空Map数据结构,遍历需要去重的数组,把数组的每一个元素作为key存到Map中。由于Map中不会出现相同的key值,所以最终得到的就是去重后的结果。{}空对象无法去重。 11.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,' asides, 'asides, {}, {}]; console.log (unique (arr))

Parsing: {} cannot be duplicated

This is the end of the article on "what are the ways to remove duplicates in an array by JS". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the methods to remove the weight of arrays in JS". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report