zoukankan      html  css  js  c++  java
  • 对象去重与数组去重

    对象去重

    对象样式:{ {}, {} }

     1 // 对象去重
     2 export const delRepeat = (data) => {
     3   let keys = Object.keys(data);
     4   let set = new Set();
     5   let arrs = [];
     6   keys.forEach(key => {
     7     set.add(data[key]);
     8     arrs.push(data[keys]);
     9     if ([...set].length !== arrs.length) {
    10       delete data[key];
    11       arrs = [...set];
    12     }
    13   });
    14   return data;
    15 };

    数组去重

    数组样式 [ {} , {} ]

     1 // 数组去重
     2 export const uniqueByKey = (arr, key) => {
     3   let hash = {};
     4   let result = arr.reduce((total, currentValue) => {
     5     if (currentValue && currentValue[key]) {
     6       if (!hash[currentValue[key]]) { // 如果当前元素的key值没有在hash对象里,则可放入最终结果数组
     7         hash[currentValue[key]] = true; // 把当前元素key值添加到hash对象
     8         total.push(currentValue); // 把当前元素放入结果数组
     9       } else {
    10         total.push({});
    11       }
    12     }
    13     return total; // 返回结果数组
    14   }, []);
    15   return result;
    16 };
  • 相关阅读:
    第 3 表格和按钮
    jQuery在线手册
    阮一峰:jQuery的几篇文章
    阮一峰:MVC、MVP和MVVM的图示
    javascript event(事件对象)详解
    图片加 alt 属性
    w3c School
    命名规范
    CSS和JS标签style属性对照表
    css 选择器
  • 原文地址:https://www.cnblogs.com/qihang0/p/13409442.html
Copyright © 2011-2022 走看看