js 数组去重

1、常用的去重方法

let arry = [1, 2, 3, 4, 7, 4, 6, 11, 7, 1, 3, 2, 4];

function duplicateRemoval(data) {

  let obj = {};

  let newArry = [];

  for (let i = 0; i < data.length; i++) {

    if (!obj[data[i]]) {

      obj[data[i]] = true;

      newArry.push(data[i])

    }

  }

  return newArry

}

2、和1区别不大

function duplicateremoval(data) {

  let obj = {};

  let newArry = [];

  data.forEach(element => {

    if (!obj[element]) {

      obj[element] = true;

      newArry.push(element)

    }

  });

  return newArry

}

3, es6 的数组去重

function duplicateRemovalEs(params) {

  return Array.from(new Set(params))

}

你可能感兴趣的:(js 数组去重)