记录vue和js操作——尽管很快实现了功能,可总感觉到不爽
需求产生的原因是:后端有一些数据是从旧平台直接迁移过来的,新平台需要根据迁移过来的数据,自动生产新的数据格式。
操作符有如下几种,分项、支路和数字配合操作符可以自定义组合,例如 [000000000001]+<00001>,带[号的表示type是branch,带<表示type是sub。
// 操作符列表 operatorList: [ '+', '-', '*', '/', '(', ')', 'inputNumber' ],
举例:[000000000001]+<00001>
解析为:
[{"type":"branch","name":"1#","value":92501,"electricAddr":"000000000001"},{"type":"+"},{"type":"sub","name":"电电电","value":69640,"number":"00001"}]
部分实现代码如下:
data () { return { // 操作符列表 operatorList: [ '+', '-', '*', '/', '(', ')', 'inputNumber' ], filterOperator: [], //操作符过滤和排序列表 } method:{ //操作符排序和过滤 geOoperatorFilter (expStr) { let arr = this.operatorList.map(n => { return { index: expStr.indexOf(n), val: n }; }); arr = arr.filter(f => f.index > -1); arr.sort(this.sortId); this.filterOperator = arr.map(m => { return m.val }); // console.log('this.filterOperator :>> ', this.filterOperator); }, sortId (a, b) { return a.index - b.index;//由低到高 }, // true:数值型的,false:非数值型 myIsNaN (value) { return typeof value === 'number' && !isNaN(value); }, //[000000000001]+[11101110011]-<1002>*<c3110> reveserExpress (expStr) { this.geOoperatorFilter(expStr); let arr = []; this.analysisData(arr, expStr); // console.log('arr :>> ', arr); let jsonArr = []; arr.forEach(n => { if (n.includes('[')) { jsonArr.push({ "type": "branch", "name": "", "value": '', "electricAddr": n.replace('[', '').replace(']', '') }); } if (n.includes('<')) { jsonArr.push({ "type": "sub", "name": "", "value": '', "number": n.replace('<', '').replace('>', '') }); } if (this.filterOperator.includes(n)) { jsonArr.push({ "type": n }); } //如果是个数字 if (this.myIsNaN(n)) { jsonArr.push({ "type": 'number', "value": n }); } }); // console.log('jsonArr :>> ', jsonArr); this.form.expressionJson = JSON.stringify(jsonArr); }, //是否包含分隔符 containOperator (word) { return this.filterOperator.some(f => word.includes(f)); }, analysisData (arr, expStr) { // debugger; this.filterOperator.forEach(n => { if (expStr.includes(n)) { // debugger; let tempArr = expStr.split(n); tempArr.forEach((t, index) => { //不包含分隔符 if (!this.containOperator(t) && !arr.includes(t)) { // debugger; arr.push(t); if (index < tempArr.length - 1) { arr.push(n); } } else { // debugger; this.analysisData(arr, t); } }) } }) }, }
测试:
let expStr = '[000000000001]-[11101110011]/<1002>+<c3110>'; this.reveserExpress(expStr);
运行结果:
[{"type":"branch","name":"","value":"","electricAddr":"000000000001"},{"type":"-"},{"type":"branch","name":"","value":"","electricAddr":"11101110011"},
{"type":"/"},{"type":"sub","name":"","value":"","number":"1002"},{"type":"+"},{"type":"sub","name":"","value":"","number":"c3110"}]
虽然很快实现了功能,可是写这样的代码总感觉好恶心,可又不知道该怎么办?好想打人,可是没有发泄对象,只好扇自己一巴掌!
众里寻他千百度,猛一回头,卧艹,那人却在一个人过马路~
博客地址: | http://www.cnblogs.com/jiekzou/ | |
博客版权: | 本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。 如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。 |
|
其它: |
.net-QQ群4:612347965
java-QQ群:805741535
H5-QQ群:773766020 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决