我想改变这个
var data = [60, rose, 40, rose1, 20, rose2];
对此
var data: [{ value: 60, name: 'rose' }, { value: 40, name: 'rose1' }, { value: 20, name: 'rose2' }];
CoderPi.. 5
你可以这样做:
var data = [60, "rose", 40, "rose1", 20, "rose2"]
var dataObj = []
for (var i = 0; i < data.length; i+=2) {
dataObj.push({
value: data[i],
name: data[i + 1]
})
}
// Demo Output
document.write(JSON.stringify(dataObj))
你可以这样做:
var data = [60, "rose", 40, "rose1", 20, "rose2"]
var dataObj = []
for (var i = 0; i < data.length; i+=2) {
dataObj.push({
value: data[i],
name: data[i + 1]
})
}
// Demo Output
document.write(JSON.stringify(dataObj))