js string类型转换成数组对象类型---eval
js string类型转换成数组对象类型
eval
该方法只接受原始字符串作为参数,如果 string 参数不是原始字符串,那么该方法将不作任何改变地返回。因此请不要为 eval() 函数传递 String 对象来作为参数。
插件原始数据
/*const data = [
{ time: "00:00", value: 80 },
{ time: '06:00', value: 70 },
{ time: '12:00', value: 120 },
{ time: '18:00', value: 90 },
{ time: '24:00', value: 168 }
];*/
后台的数据
const trainingData = () => {
return builder([{
'training': {
'xaxis': ['00:00', '06:00', '12:00', '18:00', '24:00'],
'data': [0, 30, 45, 60, 30]
}
}])
}
拼接的字符串:[{time:"00:00",value:0},{time:"00:00",value:0},{time:"06:00",value:30},{time:"12:00",value:45},{time:"18:00",value:60},{time:"24:00",value:30},{time:"24:00",value:30}]
TrainingStatus.vue?4f97:199 最后的结果数据[{time:"00:00",value:0},{time:"00:00",value:0},{time:"06:00",value:30},{time:"12:00",value:45},{time:"18:00",value:60},{time:"24:00",value:30},{time:"24:00",value:30}]
TrainingStatus.vue?4f97:200 数据类型string
TrainingStatus.vue?4f97:196 拼接的字符串:[{time:"00:00",value:0},{time:"00:00",value:0},{time:"06:00",value:30},{time:"12:00",value:45},{time:"18:00",value:60},{time:"24:00",value:30},{time:"24:00",value:30}]
TrainingStatus.vue?4f97:199 最后的结果数据[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
TrainingStatus.vue?4f97:200 数据类型object
created () { getRoleList({ t: new Date() }) getTrainingData().then(res => { this.trainingdata = res.data[0].training //心率折线数据 for(var i =0 ; i<this.trainingdata.xaxis.length ; i++){ var time1 = this.trainingdata.xaxis[i] var value1 = this.trainingdata.data[i] if(i==0){ this.databinjie = '[{'+ 'time:"' +time1 + '",' + 'value:' +value1 + '},' } this.databinjie += '{'+ 'time:"' +time1 + '",' + 'value:' +value1 + '},' var changdu = this.trainingdata.xaxis.length -1 if(i==changdu){ this.databinjie += '{'+ 'time:"' +time1 + '",' + 'value:' +value1 + '}]' } } console.log('拼接的字符串:'+this.databinjie) this.data = eval(this.databinjie) // this.data = JSON.parse(this.data) console.log('最后的结果数据'+this.data) console.log('数据类型'+typeof(this.data)) }) },