不爱学习的小码农 2023-02-22 09:45 采纳率: 12.5%
浏览 16

小程序展示解析的内容

小程序展示格式化的内容图一是小程序展示的字段,图二是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

  • 写回答

2条回答 默认 最新

  • 崽崽的谷雨 2023-02-22 10:16
    关注

    JSON.parse 不行吗

    评论

报告相同问题?

问题事件

  • 修改了问题 2月22日
  • 创建了问题 2月22日

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误