es6 배열 재 활용 (중복 대상 도 제거 가능)

3025 단어
1, 단순 유형 제거
//ES6    Set    ,     ,             ,                 , :
let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3];
 let set = new Set(array);
 console.log(set);
 // => Set {1, 2, 3, 4, 5}

//ES6 Array         Array.from,               ,   querySelectAll    HTML DOM Node List,  ES6    Set Map      , : let set = new Set();
 set.add(1).add(2).add(3);
 let array = Array.from(set);
 console.log(array);
 // => [1, 2, 3]

       //  ,                  :
      let array = Array.from(new Set([1, 1, 1, 2, 3, 2, 4]));
      console.log(array);
      // => [1, 2, 3, 4]

2, 인용 유형 제거
var arr = [{
    "name": "ZYTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "AAAAAA.doc"
}, {
    "name": "ZYTA",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "BBBBBB.doc"
}, {
    "name": "ZDTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "CCCCCC.doc"
}, {
    "name": "ZYTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "AAAAAA.doc"
}];
var hash = {};
arr = arr.reduce(function(item, next) {
    hash[next.name] ? '' : hash[next.name] = true && item.push(next);
    return item
}, [])
console.log(arr);

다음으로 전송:https://juejin.im/post/5b17a2c251882513e9059231

좋은 웹페이지 즐겨찾기