字符串转数组,然后判断是否是数组中的元素

    // 处理一个元素是否是数组中的元素 value必须相等
    isInArray(str,value){
        var arr = [];
        if(str||str==0){
            arr = str.split(",");
        }else{
            return false;
        }
        for(var i = 0; i < arr.length; i++){
            // 不用全等比较
            if(value == arr[i]){
                return true;
            }
        }
        return false;
    } 

    handleThresholdStatus(series) {
        series.thresholds = StatusPluginCtrl.parseThresholds(this.panel);
        series.inverted = series.thresholds.crit < series.thresholds.warn;
        series.display = this.panel.display;

        let isCritical = false;
        let isWarning = false;
        let isCheckRanges = series.thresholds.warnIsNumber && series.thresholds.critIsNumber;
        if (isCheckRanges) {
            if (!series.inverted) {
                if (series.display_value >= series.thresholds.crit) {
                    isCritical = true
                } else if (series.display_value >= series.thresholds.warn) {
                    isWarning = true
                }  
            } else {
                if (series.display_value <= series.thresholds.crit) {
                    isCritical = true
                } else if (series.display_value <= series.thresholds.warn) {
                    isWarning = true
                }
            }
        } else {
            // 之前走的逻辑 是等值比较
            // if (series.display_value == series.thresholds.crit) {
            //  isCritical = true
            // } else if (series.display_value == series.thresholds.warn) {
            //  isWarning = true
            // }
             
            // 改之后的逻辑 非数字进入该步骤 进行多元素循环等值比较
            // 为了不影响其他的功能,就只判断String Threshold单独走这个新加的逻辑,其他的还是走之前的逻辑
            // 如果选中的是 String Threshold 就判断是否是 这个数组中得某一个元素
            // 优先crit crit等级高于warn
            if(this.panel.valueHandler == 'String Threshold'){
                // indexOf 字符串使用存在问题 如 'index' 也能匹配到 'index0,index1',而只想匹配到'index0'
                // if (series.thresholds.crit && series.thresholds.crit.indexOf(series.display_value)>0) {
                if (this.isInArray(series.thresholds.crit, series.display_value)) {
                    isCritical = true;
                // } else if (series.thresholds.warn && series.thresholds.warn.indexOf(series.display_value)>0) {
                } else if (this.isInArray(series.thresholds.warn, series.display_value)) {
                    isWarning = true;
                }
            }else{
                if (series.display_value == series.thresholds.crit) {
                    isCritical = true
                } else if (series.display_value == series.thresholds.warn) {
                    isWarning = true
                }
            }
            
        }

        // Add units-of-measure and decimal formatting or date formatting as needed
        series.display_value = this.formatDisplayValue(series.display_value);

        if(isCritical) {
            this.crit.push(series);
            series.displayType = this.displayTypes[0]
        } else if(isWarning) {
            this.warn.push(series);
            series.displayType = this.displayTypes[0]
        } else if (series.display) {
            if(series.displayType == "Annotation") {
                this.annotation.push(series);
            } else {
                this.display.push(series);
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_34292924/article/details/87068562
今日推荐