JS數組&&數組對象去重


數組去重

面試中經常問的一道題,使用JS寫一個函數,對數組進行去重。

1.使用ES6的new Set()方式

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    let arr = [1,1,2,2,3,3,4,4,5,5];
    function unique(arr){
      return new Set(arr)
    }
    let res = unique(arr);
    console.log(res);
  </script>
</body>
</html>

 

 

2.使用數組的indexOf方法進行去重

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    let arr = [1,1,1,2,3,3,4,11,112,332,332,2,2,3,3,4,4,5,5];
    /* 
      思路:
        1.創建一個空的數組
        2.對arr的數據進行循環遍歷,判斷空數組是否有arr的值,沒有就push進去
    */  
    let uniqueArr = [];
    arr.map(item=>{
      // console.log(item);
      uniqueArr.indexOf(item) == -1 ? uniqueArr.push(item) : "";
    })
    console.log(uniqueArr);
  </script>
</body>
</html>

 

數組對象去重

 1.使用reduce對數組對象去重

let log = console.log.bind(console);
let person = [
     {id: 0, name: "小明"},
     {id: 1, name: "小張"},
     {id: 2, name: "小李"},
     {id: 3, name: "小孫"},
     {id: 1, name: "小周"},
     {id: 2, name: "小陳"},   
];

let obj = {};

person = person.reduce((cur,next) => {
    obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
    return cur;
},[]) //設置cur默認類型為數組,並且初始值為空的數組
log(person);

 

2.使用reduce對數組字符串去重且保留2個相同的屬性值

去重前:

 

 

去重后:

 

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    let person = [{
        'age': 41,
        'id': 12,
      },
      {
        'age': 20,
        'id': 12,
      },
      {
        'age': 23,
        'id': 12,
      },
      {
        'age': 25,
        'id': 15,
      },
      {
        'age': 30,
        'id': 15,
      },
      {
        'age': 12,
        'id': 20,
      },
    ];
    let log = console.log.bind(console.log);
    let obj = {};
    let obj2 = {};
    let peon2 = [];
    let peon = person.reduce((cur, next) => {
      // 這里三元表達式的""表示什么也不操作   這里的next'id'就意味着這個程序就是根據next.'id'進行去重;
      // obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
      if (obj[next.id]) {
        // 判斷數組對象里面的ID是否存在,如果存在,就追加到peon2數組中
        peon2.push(next);
      } else {
        obj[next.id] = true && cur.push(next);
      }
      // log(obj);
      return cur;
    }, []) // 設置cur默認類型為數組,並且返回值為空的數據

    // 對數據再次進行去重
    peon2 = peon2.reduce((cur, next) => {
      if (obj2[next.id]) {
        peon2.push(next);
      } else {
        obj2[next.id] = true && cur.push(next);
      }
      return cur;
    }, [])
    peon2.map(item => {
      peon.push(item);
    })
    log(peon);
  </script>
</body>

</html>

 

更多資料:

  • https://www.cnblogs.com/caideyipi/p/7679681.html
  • https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM