码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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;
    },
    

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

  • 相关阅读:
    力扣每日一题:895. 最大频率栈【哈希表和队列】
    PhotoSweeper X mac版 v4.8.5 相似重复照片清理工具 兼容 M1/M2
    Interlay采用Moonbeam路由流动性,为波卡发展更多流动性
    【LeetCode力扣】189 53 轮转数组 | 最大子数组和
    Cloud微服务:Ribbon负载均衡
    《算法导论》15.2 矩阵链乘法(含有C++代码)
    Springboot+vue的应急救援物资管理系统,Javaee项目,springboot vue前后端分离项目。
    springboot的@ConditionalOnClass注解
    node.js学习之数据库及基本操作、SQL语句
    算法通关村第六关-青铜挑战树
  • 原文地址:https://blog.csdn.net/weixin_42678822/article/details/139305360
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号