Home  >  Article  >  Web Front-end  >  What are the methods to remove duplicates from js arrays? Summary of js array deduplication methods (example)

What are the methods to remove duplicates from js arrays? Summary of js array deduplication methods (example)

不言
不言Original
2018-08-17 17:04:112227browse

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.

The most basic way of writing is using indexOf()

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;
}

The removal method that seems to have good performance

// 通过遍历每次被值给到数组角标 适用于数组里面数量不太的数组
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;
}

Alternative way of writing

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
}

ES5 filter filter function

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));

ES6 Set This is simply filtering and sorting for filtering

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

We can implement it with one line of code

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 array deduplication

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn