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"];