小程序展示解析的内容

小程序展示格式化的内容图一是小程序展示的字段,图二是web端展示的字段,我小程序不能使用new function 请问还能使用别的方法将这个内容给解析出来吗

img

img

 for (let i = 0; i < this.measureArr.length; i++) {
                this.realValueArr.map(item => {
                    if (this.measureArr[i].measureId == item.measureId) {
                        if (this.measureArr[i].sampleTime !== item.sampleTime) this.measureArr[i].sampleTime = item.sampleTime;
                        if (this.measureArr[i].sampleValue !== item.sampleValue) this.measureArr[i].sampleValue = item.sampleValue;
                    }
                });
            }
            this.Info = this.measureArr;
            console.log(JSON.parse(JSON.stringify(this.Info)));
            console.log(JSON.parse(JSON.stringify(this.Info[8].printf)));

这个是我同事给我的web端格式化代码但是使用了new function
/*
 * 格式化显示
 * val: 采样值
 * fmt: 格式化内容
*/
export function formatDisplay(val, fmt){
    console.log("val",val);
    console.log("fmt",fmt);
    if(val === undefined || val === null || val === '') return null;
    if(!fmt) return val;
    if(fmt.type == 0){
        if(!fmt.script){
            return val
        }
        // 脚本模式
        if(fmt.script.indexOf('\br') != -1 || fmt.script.indexOf('\\br') != -1) {
            fmt.script = fmt.script.replace(new RegExp('\\\\br', 'gm'), '<br />')
        }
        var func = new Function("$val", fmt.script);
        var state = func(+val);
        return state || val;
    }
    else if(fmt.type == 1){
        // 保留小数位
        return (val*1).toFixed(fmt.scale);
    }
    else if(fmt.type == 2){
        // 按位解析
        var state = '';
        var split = fmt.split || ",";
        for(var bit in fmt){
            if(!isNaN(bit)){
                var x = Math.pow(2, bit);
                if((val&x) == x){
                    if(state == '')
                        state = fmt[bit];
                    else
                        state += split + fmt[bit];
                }
            }
        }
        return state || fmt["default"] || val;
    }
    else if(fmt.type == 3){
        // 按值解析
        return fmt[String(val)] || fmt["default"] || val;
    }
    else if(fmt.type == 4){
        // 拆分显示
        var state = '';
        var split = fmt.split || ",";
        var items = fmt.items;
        if(Array.isArray(items) && items.length){
            for(var i = 0; i < items.length; i++){
                var item = items[i];
                var from = item.from;
                var to = item.to || from;
                var x = Math.pow(2,to-from+1)-1;
                var v = val>>from&x;
                if(item.gain) v *= item.gain;
                if(item.scale) v = v.toFixed(item.scale);
                var unit = item.unit || '';
                var name = item.name ? item.name + ':' : '';
                if(state == '')
                    state = `${name}${v}${unit}`;
                else
                    state += split + `${name}${v}${unit}`;
            }
        }
        return state || val;
    }

    throw "无法识别类型:"+fmt.type;
}

这个this.info就是我需要展示的数据 需要解析的字段是printf

JSON.parse 不行吗

如果想要过滤显示状态的话,取一个type,然后根据数据字典查一下对相应关系就行了吧?