2021-js++全修班-->53.数组去重(全网最全的)

264 阅读1分钟

js++全修班-->53.数组去重(1)

53数组去重若干方法1

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>53数组去重若干方法</title>
</head>
<body>

<script>
  let arr = [1,2,3,4,44,42,21,3,3,2,4,5,5,3,2,2]

  function uniqueArr(array) {
    let _arr = [],
        isRepeat;
    for (let i = 0; i < array.length; i++) {
      isRepeat = false;
      for (let j = 0; j < _arr.length; j++) {
        if (_arr[j] === array[i]){
          isRepeat = true
          break;
        }
      }
      if (!isRepeat){
        _arr.push(array[i])
      }
    }

    return _arr.sort((a,b)=>{
      return a-b
    })

  }

  console.log(uniqueArr(arr));

</script>
</body>
</html>

53数组去重若干方法2

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>53数组去重若干方法2</title>
</head>
<body>

<script>
  let arr = [1,2,3,4,44,42,21,3,3,2,4,5,5,3,2,2]
  function uniqueArr(array) {
    return array.filter(function (item,index) {
      return array.indexOf(item) === index
    })
  }

  let newArr = uniqueArr(arr).sort(function (a,b) {
    return a-b
  });

  console.log(newArr);


</script>
</body>
</html>

53数组去重若干方法3

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>53数组去重若干方法3</title>
</head>
<body>

<script>
  let arr = [1,2,3,4,44,42,21,3,3,2,4,5,5,3,2,2]
  function uniqueArr(array) {
    return array.filter(function (item,index) {
      return array.indexOf(item) === index
    })
  }

  let newArr = uniqueArr(arr).sort(function (a,b) {
    return a-b
  });

  console.log(newArr);


</script>
</body>
</html>

53数组去重若干方法4

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>53数组去重若干方法4</title>
</head>
<body>

<script>
  let arr = [1, 2, 3, 4, 44, 42, 21, 3, 3, 2, 4, 5, 5, 3, 2, 2]

  function uniqueArr(array) {
    let _arr = []
    array.forEach(function (item) {
      /*
      *  indexOf 返回回来的是, -1   index
      *  include 返回回来的是: true false
      *
      * */
      if (!_arr.includes(item)){
        _arr.push(item)
      }
    })
    return _arr
  }
  let newArr = uniqueArr(arr).sort(function (a, b) {
    return a - b
  });

  console.log(newArr);


</script>
</body>
</html>

53数组去重若干方法5

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>53数组去重若干方法2</title>
</head>
<body>

<script>
  let arr = [1, 2, 3, 4, 44, 42, 21, 3, 3, 2, 4, 5, 5, 3, 2, 2]

  function uniqueArr(array) {
    return array.sort(function (a, b) {
      return a - b
    }).reduce(function (prev,item) {
      if (prev.length === 0 || prev[prev.length - 1] !== item){
        prev.push(item)
      }
      return prev

    },[]);
  }


  console.log(uniqueArr(arr));


</script>
</body>
</html>

53数组去重若干方法6

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>53数组去重若干方法6</title>
</head>
<body>

<script>
  let arr = [1, 2, 3, 4, 44, 42, 21, 3, 3, 2, 4, 5, 5, 3, 2, 2]

  function uniqueArr(array) {
    let _arr = [],
      _temp = new Map()
    /*
    * 这里的方法是利用
    * _temp = {
    *   1:1,
    *   2:1,....
    * }
    *
    * _arr ==> [1,2...]
    *
    * */
    for (let i = 0; i < array.length; i++) {
      if (!_temp.get(array[i])){
        _temp.set(array[i],1)
        _arr.push(array[i])
      }
    }
    return _arr
  }

  let newArr = uniqueArr(arr).sort(function (a, b) {
    return a - b
  });

  console.log(newArr);


</script>
</body>
</html>

53数组去重若干方法7

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>53数组去重若干方法2</title>
</head>
<body>

<script>
  let arr = [1, 2, 3, 4, 44, 42, 21, 3, 3, 2, 4, 5, 5, 3, 2, 2]

  function uniqueArr(array) {
    let _arr = [],
      _temp = {}
    for (let i = 0; i < array.length; i++) {
      if (!_temp[array[i]]){
        _temp[array[i]] = 1
        _arr.push(array[i])
      }
    }
    return _arr
  }

  let newArr = uniqueArr(arr).sort(function (a, b) {
    return a - b
  });

  console.log(newArr);


</script>
</body>
</html>

53数组去重若干方法8

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>53数组去重若干方法2</title>
</head>
<body>

<script>
  let arr = [1, 2, 3, 4, 44, 42, 21, 3, 3, 2, 4, 5, 5, 3, 2, 2]

  function uniqueArr(array) {
    /*
    * new Set()  对象变数组 Array.from()
    * */
    return Array.from(new Set(array))
  }


  let newArr = uniqueArr(arr).sort(function (a, b) {
    return a - b
  });

  console.log(newArr);


</script>
</body>
</html>