首页 > 其他分享 >JS数组对象去重同时判断两个属性是否相同

JS数组对象去重同时判断两个属性是否相同

时间:2023-01-05 18:24:13浏览次数:55  
标签:const incubationTimeAdjustMonth productGroup JS 数组 CSB WB 属性

参考:https://blog.csdn.net/m0_58849641/article/details/124750983?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-1-124750983-blog-125990334.pc_relevant_3mothn_strategy_and_data_recovery&spm=1001.2101.3001.4242.2&utm_relevant_index=4

  • 判断如下的对象数组中某两个属性是否有重复记录:
[{productGroup: 'WB', incubationTimeAdjustMonth: '1月'}
{productGroup: 'WB', incubationTimeAdjustMonth: '2月'}
{productGroup: 'WB', incubationTimeAdjustMonth: '3月'}
{productGroup: 'WB', incubationTimeAdjustMonth: '4月'}
{productGroup: 'CSB', incubationTimeAdjustMonth: '1月'}
{productGroup: 'CSB', incubationTimeAdjustMonth: '2月'}
{productGroup: 'CSB', incubationTimeAdjustMonth: '3月'}
{productGroup: 'CSB', incubationTimeAdjustMonth: '4月'}
{productGroup: 'ICB', incubationTimeAdjustMonth: '1月'}
{productGroup: 'ICB', incubationTimeAdjustMonth: '2月'}
{productGroup: 'ICB', incubationTimeAdjustMonth: '3月'}
{productGroup: 'ICB', incubationTimeAdjustMonth: '4月'}
{productGroup: 'ZNB', incubationTimeAdjustMonth: '1月'}
{productGroup: 'ZNB', incubationTimeAdjustMonth: '2月'}
{productGroup: 'ZNB', incubationTimeAdjustMonth: '3月'}
{productGroup: 'ZNB', incubationTimeAdjustMonth: '4月'}
{productGroup: 'WB', incubationTimeAdjustMonth: '4月'}
{productGroup: 'WB', incubationTimeAdjustMonth: '5月'}
{productGroup: 'WB', incubationTimeAdjustMonth: '6月'}
{productGroup: 'CSB', incubationTimeAdjustMonth: '5月'}
{productGroup: 'CSB', incubationTimeAdjustMonth: '6月'}]

 const newArr = this.f_CompareData(checkPlanSetIncubationAll);//去除重复记录并得到一个新数组
 // 比较新的数组与原来的数组长度是否一致,不一致就说明有重复记录
 if(newArr.length!=checkPlanSetIncubationAll.length)
 {
      //有重复记录
 }
// 对象数组去重
    f_CompareData(arr)
    {
      const cache =[];
      for(const t of arr)
      {
        // 检查是否存在
        if(cache.find(c=>c.productGroup == t.productGroup&& c.incubationTimeAdjustMonth==t.incubationTimeAdjustMonth))
        {
          continue;
        }
        cache.push(t);        
      }
      return cache;
    }
  • 注意这种方式只能判断对象数组中某个属性值是否有重复:
  // 判断数组中有无重复记录
  // const newArr = checkPlanSetIncubationAll.map(item =>item.productGroup&&item.incubationTimeAdjustMonth);//好像只能使用一个属性,这里两个只有后面的生效了,这里只得到了一个属性值的新数组
  // const arrSet = new Set(newArr);//数组去重
  // if(arrSet.size !== newArr.length){
  //     flag.code=3;
  //     flag.message='同一个产品分组和同一个月份不能重复,请检查';
  // }
  • 例子:



标签:const,incubationTimeAdjustMonth,productGroup,JS,数组,CSB,WB,属性
From: https://www.cnblogs.com/newcapecjmc/p/17028543.html

相关文章

  • hdu: 张煊的金箍棒(3)(树状数组的区间修改,区间查询)
    ProblemDescription张煊的金箍棒升级了!升级后的金箍棒是由N段相同长度的金属棒连接而成(最开始每段金属棒的价值都是1,从1到N编号);张煊作为金箍棒的主人,可以对金箍棒任意......
  • computed属性
    计算属性就是当依赖的值发生变化的时候,才会触发它的更改,如果依赖的值不发生变化的时候,使用的是缓存中的值简易的购物车Tips:利用函数的形式来完成数值的变化<template>......
  • Adobe AIR * 设置移动应用程序属性
    ​ 对于其他AIR应用程序,可以在应用程序描述符文件中设置基本应用程序属性。移动应用程序会忽略某些特定于桌面的属性,例如窗口大小和透明度。移动应用程序还可以使用自己......
  • JS笔记
    1、参考JavaScript面试攻略​​JavaScript参考​​2、​​js——引用类型和基本类型​​基本类型:NumberBooleanStringundefinednull引用类型:ObjectFunction3、清除Ch......
  • Jmeter BeanShell处理JSON响应
    一:Json响应如下:二:添加beanshell后置处理器  三:Json处理代码如下:importcom.alibaba.fastjson.*;publicstaticStringline_code_list(Stringjson_text){  ......
  • JSOI2009 题解
    Count二维树状数组板子题,开\(c\)个二维树状数组即可过。可以通过离线对每个权值单独操作做到只开一个二维树状数组。如果空间要求更紧的话可以cdq分治做到只开一个......
  • 【numpy】创建ndarray数组
    创建数组ndarray构造器创建importnumpyasnp'''使用底层ndarray构造器来创建参数说明:名称 描述object 数组或嵌套的数列dtype 数组元素的数据类型,可选cop......
  • 前端利用formData格式进行数据上传,前端formData 传值 和 json传值的区别?
    前端利用formData格式进行数据上传,前端formData传值和json传值的区别? contentType常见的格式text/plain:纯文本格式application/json:JSON数据格式application/......
  • IM通讯协议专题学习(七):手把手教你如何在NodeJS中从零使用Protobuf
    1、前言Protobuf是Google开源的一种混合语言数据标准,已被各种互联网项目大量使用。Protobuf最大的特点是数据格式拥有极高的压缩比,这在移动互联时代是极具价值的(因为移动......
  • toastr.js 便捷弹框怎么用?怎么本地化?
    〇、简介toastr.js是一个非常简洁的弹窗消息插件,主要原因就是其脚本和样式文件较小。并且可以根据自己的需求,修改样式文件,可以应用在多种不同的场景。https://codeseve......