JS array array object to heavy &&

Deduplication Array

Interviews often asked a question, use JS to write a function, go heavy on the array.

1. Use ES6 the new Set () mode

<!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. The method of using the array will be indexOf deduplication

<! DOCTYPE HTML> 
<HTML lang = "EN"> 
<head> 
  <Meta charset = "UTF-. 8"> 
  <Meta name = "the viewport" Content = "width = Device-width, Initial-Scale = 1.0"> 
  < HTTP-equiv = Meta "X--the UA-Compatible" Content = "IE = Edge"> 
  <title> the Document </ title> 
head </> 
<body> 
  <Script> 
    the let ARR = [1,1,1,2, 3,3,4,11,112,332,332,2,2,3,3,4,4,5,5 ];
     / *  
      ideas: 
        1. Create an empty array 
        2. whether the data cycle through arr, empty array is determined has a value arr no push it into 
    * /   
    the let uniqueArr = []; 
    arr.map (Item =>{
      // console.log(item);
      uniqueArr.indexOf(item) == -1 ? uniqueArr.push(item) : "";
    })
    console.log(uniqueArr);
  </script>
</body>
</html>

 

Deduplication Array object

 1. Use of an array of objects to reduce the weight

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;
},[]) // Set default type array cur, and the initial value of the empty array 
log (person);

 

2. Use of an array of strings to reduce weight and to retain the same attribute value 2

Go before re-:

 

 

Go after re-:

 

<!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 is ,
         'ID': 20 is , 
      }, 
    ]; 
    the let log = console.log.bind (Console .log); 
    the let obj = {}; 
    the let obj2 = {}; 
    the let peon2 = []; 
    the let Peon = person.reduce ((CUR, Next) => {
       // here a triplet of expressions of "" What is also expressed does not operate here next'id 'means that this program is based on next.'id' de re; 
      // obj [next.id] "":? obj [the Next.id] = true && cur.push(next);
      IF (obj [next.id]) {
         // Analyzing the array or object ID exists, if present, are appended to the array peon2 
        peon2.push (Next); 
      } the else { 
        obj [next.id] = to true && CUR .push (Next); 
      } 
      // log (obj); 
      return cur; 
    }, []) // set default type array cur, and null values are returned data 

    // data again to re 
    peon2 = peon2. the reduce ((CUR, Next) => {
       IF (obj2 [next.id]) { 
        peon2.push (Next); 
      } the else { 
        obj2 [next.id] = to true &&  cur.push (Next);
      }
      return cur;
    }, [])
    peon2.map(item => {
      peon.push(item);
    })
    log(peon);
  </script>
</body>

</html>

 

more info:

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

Guess you like

Origin www.cnblogs.com/sauronblog/p/11531277.html