当前位置: 首页 > news >正文

20240529每日前端--------vue数组对象包含数组,判断子数组是否有重复对象

数组格式如下:

"industrySceneList": [
    {
        "mainIndustry": 1,
        "mainIndustryName": "林草",
        "sceneList": [
            {
                "subIndustry": 1,
                "subIndustryName": "森林防火"
            }
        ]
    },
    {
        "mainIndustry": 2,
        "mainIndustryName": "国土",
        "sceneList": [
            {
                "subIndustry": 141,
                "subIndustryName": "其它-1"
            },
            {
                "subIndustry": 11,
                "subIndustryName": "耕地保护"
            },
            {
                "subIndustry": 12,
                "subIndustryName": "矿山保护"
            }
        ]
    },
    {
        "mainIndustry": 12,
        "mainIndustryName": "其它-2",
        "sceneList": [
            {
                "subIndustry": 71,
                "subIndustryName": "其它-3"
            }
        ]
    }
],

第一种办法:通过map计数来判定:

onSubIndustryChange(industrySceneList) {
  let validIndust = true;
  let subIndustry = "";
  // 创建一个Map来存储subIndustry及其出现次数
  const subIndustryCounts = new Map();

  // 遍历industrySceneList
  industrySceneList.forEach(industryScene => {
    // 遍历当前场景列表中的sceneList
    industryScene.sceneList.forEach(scene => {
      // 获取当前subIndustryName的计数,如果不存在则默认为0
      const count = subIndustryCounts.get(scene.subIndustryName) || 0;
      // 将计数加1
      subIndustryCounts.set(scene.subIndustryName, count + 1);
    });
  });

  // 筛选出计数大于1的subIndustry,即重复的subIndustry
  const duplicates = Array.from(subIndustryCounts)
      .filter(([subIndustry, count]) => count > 1)
      .map(([subIndustry, _]) => subIndustry);

  //数组转String用逗号拼接
  subIndustry = duplicates.join(',');
  if (duplicates.length > 0) {
    validIndust = false;
    this.$message({
      message: '当前存在' +subIndustry+'重复,请检查后重试',
      type: 'error'
    });
  }
  return validIndust;
},

第二种办法:通过ES6 set数据类型

hasDuplicateIds(value) {
  console.log(value);
  let dupValid = true;
  const nonMinusOneItems = this.industryScenario.filter(item => item.mainIndustry == value);
  const uniqueNonMinusOneIds = new Set(nonMinusOneItems.map(item => item.mainIndustry));

  if(nonMinusOneItems.length !== uniqueNonMinusOneIds.size){
    this.$message({
      message: '行业不能重复',
      type: 'error'
    });
    dupValid = false;
  }
  return dupValid;
},

---------- 兄弟们更喜欢哪一种呢~

相关文章:

  • 计算机网络- 特定服务类型(Type of Service, TOS) 服务质量(Quality of Service, QoS)
  • express处理get请求和post请求
  • K8s service 进阶
  • Windows驱动开发系列文章一
  • MySQL的安全性
  • 《TCP/IP网络编程》(第十二章)I/O复用(2)
  • 【微机原理及接口技术】可编程计数器/定时器8253
  • 继电器的选型和英应用
  • 解决文件传输难题:如何绕过Gitee的100MB上传限制
  • 基于springboot+vue的社区医院管理服务系统
  • 蓝桥杯-AB路线(详细原创)
  • C#多线程同步lock、Mutex
  • 深入分析 Android Activity (一)
  • CSS变量 -- var() 使用教程
  • C# 枚举高阶
  • docker容器中解决中文乱码
  • markdown语法保存
  • 使用C语言实现学生信息管理系统
  • PS:电子书App自动截图后合成一个PDF文档
  • 【本地运行chatgpt-web】启动前端项目和service服务端项目,也是使用nodejs进行开发的。两个都运行成功才可以使用!
  • 四川苍溪县教育局通报“工作人员辱骂举报学生”:停职检查
  • 湖南小伙“朱雀玄武敕令”提交申请改名为“朱咸宁”
  • 文化体验+商业消费+服务创新,上海搭建入境旅游新模式
  • 应勇:以法治力量服务黄河流域生态保护和高质量发展
  • 财政部:前3月国有企业利润总额10907.4亿元,同比增1.7%
  • “网红”谭媛去世三年:未停更的账号和困境中的家庭