Home >Web Front-end >JS Tutorial >What are the methods to remove duplicates from js arrays? Summary of js array deduplication methods (example)
What this article brings to you is what are the methods for deduplicating js arrays? A summary of js array deduplication methods (examples) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
var arr = [1,1,5,77,32,54,2,4,5,2,2,4,52,2,2,2,2,2] //比较常规的语法使用indexOf来判断是否已经存在 getFileArray(arr) function getFileArray(arr){ var array = []; arr.forEach(e => { if(array.indexOf(e) !== -1){ return; }else { array.push(e) } }); return array; }
// 通过遍历每次被值给到数组角标 适用于数组里面数量不太的数组 var arr = [1,1,5,77,32,54,2,4,5,2,2,4,52,2,2,2,2,2] getFilterArray(arr) function getFilterArray (array) { const res = []; const json = {}; for (let i = 0; i < array.length; i++){ const _self = array[i]; //获取迭代的数值 if(!json[_self]){ //假如json在_self这个下标没有数值,就说明这个数据没有 res.push(_self); //没有的话就push json[_self] = 1; //同时给这和匹配不到的下标一个值,保证下次进不来 } // 这样写的话 不需要循环遍历 对性能要求更小 } return res; }
var array = [1, 1, 5, 77, 32, 54, 2, 4, 5, 2, 2, 4, 52, 2, 2, 2, 2, 2] arrtoObject(arr) function arrtoObject(arrs) { //var obj={}; var obj = new Object(); for (var i = 0; i < arrs.length; i++) { obj[arrs[i]] = true; } objectToarr(obj) } function objectToarr(obj){ console.log(obj); var arr = []; for (const i in obj) { arr.push(i) } console.log(arr); return arr }
var array = [1,1,5,77,32,54,2,4,5,2,2,4,52,2,2,2,2,2] function unique(array) { var res = array.filter(function (item, index, array) { return array.indexOf(item) === index; //因为array.indexOf返回数组的下标 如果这里的下标和index不一样说明已经存在了,就直接退出了 }) return res; } console.log(unique(array));
var arr = [1, 1, 5, 77, 32, 54, 2, 4, 5, 2, 2, 4, 52, 2, 2, 2, 2, 2] function FilterArray(arr) { set = new Set(arr) let arrays = Array.from(set) arrays.sort((a, b) => { return a - b }) return arrays } FilterArray(arr)
If we only need to sort
var arr = [1, 1, 5, 77, 32, 54, 2, 4, 5, 2, 2, 4, 52, 2, 2, 2, 2, 2] var FilterArray = (arr) => [...new Set(arr)] // FilterArray(arr)
Related Recommended:
JS uses the indexOf() method to implement array deduplication
The above is the detailed content of What are the methods to remove duplicates from js arrays? Summary of js array deduplication methods (example). For more information, please follow other related articles on the PHP Chinese website!