jsstring类型转换成数组对象类型---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 => {
//⼼率折线数据
for(var i =0 ; i&ainingdata.xaxis.length ; i++){
var time1 = ainingdata.xaxis[i]
var value1 = ainingdata.data[i]
if(i==0){
this.databinjie = '[{'+ 'time:"' +time1 + '",' + 'value:' +value1 + '},'
}
this.databinjie += '{'+ 'time:"' +time1 + '",' + 'value:' +value1 + '},' var changdu = ainingdata.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))
})
},es6字符串转数组

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。