EL表达式的集合或者普通JS字符串在JS中转化为对象数组

这里写自定义目录标题

EL表达式转化Js对象数组

${mbxlist}
<p id="q">1212</p>
<script>
    const p = document.getElementById("q");
    const str = "${sessionScope.mbxlist}";
    //封装一个函数,将el表达式,转换成一个对象,因为方便获取属性
   function nbplus_0(str) {
        let arr1 = [];
        let arr2 = [];
        let arr3 = [];
        let wd = new RegExp("'", "g");
        let str1 = str.replace(wd, "");
        arr1 = str1.split("},");
        arr1[arr1.length-1]=arr1[arr1.length-1].replace("}]","");
        for (let i = 0; i < arr1.length; i++) {
            arr2 = arr1[i].split("{")[1].split(", ");
            let obj = {};
            for (let j = 0; j < arr2.length; j++) {
                let key = arr2[j].split("=")[0];
                let value = arr2[j].split("=")[1];
                obj[key] = value;
            }
            arr3[i] = obj;
        }
        return arr3;
    }
console.log(nbplus_0(str));
</script>

在这里插入图片描述

©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页