js使用map,JS中String转Json方法
js使用map
使用map时遇到的报错:
下面的代码报错TypeError: data.map is not a function
let nameList = data.map(item => item.name);
原因1:data不是数组
解决方法:将data转换为数组
let data = Array.from(data);
正常使用:
getData() { // 调用接口方法 getlistInspectionnum().then(response => { console.log("######234#######") // console.log(response.length) // console.log("23242:" + response[0]["deviceCompleted"]) // if (response.length != 0) { // this.mychart3_x_data_arr.push(response[0]["deviceCompleted"]) // } else { // this.mychart3_x_data_arr.push(0) // } // this.mychart3_x_data_arr.push(response[0]["deviceCompleted"]) this.mychart3_x_data_arr = response.map(item => { return item.deviceCompleted } ); // console.log(mychart3_x_data_arr); this.mychart3_y_ywc_data_arr = response.map(item => { return item.inspectionName }); this.initOption(); }) }
JS中String转Json方法
JSON.parse(jsonstr); //可以将json字符串转换成json对象
this.inline_I = JSON.parse(response.deviceData)["Current"]; this.inline_U = JSON.parse(response.deviceData)["Voltage"];
分类:
js
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探