2021-04-02

JS数据去重

记录三种类型数据去重的处理方法

1、单个数组内部
// 去重,对{}无效var string= [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}]console.log('去重前的数据' + JSON.stringify(string)))string = Array.from(new Set(string))console.log('去重后的数据' + JSON.stringify(string)))// [1, 'true', true, 15 ,false, undefined, null, NaN, 'NaN', 0, 'a',{},{}]
2、根据数组对象单个属性
var arr = [ { from:'张三', to: '河南' }, { from:'王二', to: '阿里' }, { from:'王二', to: '杭州' }, { from:'王二', to: '山东' },]function unique(arr1) { const res = new Map(); return arr1.filter((a) => !res.has(a.from) && res.set(a.from, 1))}
3、根据数组对象多个对象
let arr = [  {  maxDeptCode: "md3",  maxDeptName: "泡泡",  minDeptCode: "md301",  minDeptName: "泡泡少儿",  schoolId: 1,  schoolName: "北京",  },  {  maxDeptCode: "md2",  maxDeptName: "中学",  minDeptCode: "md201",  minDeptName: "中学一对一",  schoolId: 1,  schoolName: "北京",  }, {  maxDeptCode: "md3",  maxDeptName: "泡泡",  minDeptCode: "md301",  minDeptName: "泡泡少儿",  schoolId: 1,  schoolName: "北京",  }, ]; function process(arr) {  // 缓存用于记录  const cache = [];  for (const t of arr) {  // 检查缓存中是否已经存在  if (cache.find(c => c.maxDeptCode === t.maxDeptCode && c.minDeptCode === t.minDeptCode)) {   // 已经存在说明以前记录过,现在这个就是多余的,直接忽略   continue;  }  // 不存在就说明以前没遇到过,把它记录下来  cache.push(t);  }  // 记录结果就是过滤后的结果  return cache; } console.log(process(arr));








原文转载:http://www.shaoqun.com/a/661743.html

跨境电商:https://www.ikjzd.com/

鸥鹭:https://www.ikjzd.com/w/2568

贝贝官网:https://www.ikjzd.com/w/1321


记录三种类型数据去重的处理方法1、单个数组内部//去重,对{}无效varstring=[1,1,'true','true',true,true,15,15,false,false,undefined,undefined,null,null,NaN,NaN,'NaN',0,0,'a','a',{},{}]console.
epa认证:https://www.ikjzd.com/w/1769
慧聪商务网:https://www.ikjzd.com/w/1836
网易考拉海购大促:https://www.ikjzd.com/w/1052
办公室老板让我害羞不已 口述我和外国老板的故事:http://lady.shaoqun.com/a/275108.html
每天被军人男友哄着入眠好幸福 口述和军人的艳遇经历:http://www.30bags.com/m/a/250758.html
雅玛森:https://www.ikjzd.com/w/1395

No comments:

Post a Comment