- 从后端获取的数据形式
const scoreData = [
{
1: 20,
2: 20,
3: 20,
4: 20,
5: 20,
},
{
1: 40,
2: 40,
3: 40,
4: 40,
5: 40,
}
]
const title = ['名字1','名字2']
将对象中的所有值取出来组成新的数组dataArr
const dataArr = []
for (var i = 0; i < scoreData.length; i++) {
dataArr.push(Object.values(scoreData[i])) // 取出scoreData中每一个对象属性的值组成一个新的数组
}
console.log(dataArr)
//打印结果应为:
[
[20,20,20,20,20],
[40,40,40,40,40]
]
将一个数组转化为一个对象
const dataObj = { ...dataArr } // 将一个数组[]转化为一个对象{}
//打印结果应为:
{
[20,20,20,20,20],
[40,40,40,40,40]
}
定义一个需要的数据形式
const allDataArr = [{ type: 'line', name: '', data: [] }]
//里面的{}对象需要多少组数据就要在最开始写出多少组数据
//但是可以动态添加
ImgArr = ['xxxx', 'yyyy', 'zzzz', 'wwww']
const dataArr = [{ leftHead: ImgArr[0] }, { leftFoot: ImgArr[1] }, { rightHead: ImgArr[2] }, { rightFoot: ImgArr[3] }]
动态添加
const allDataArr = []
for (let i = 0; i < scoreData.length; i++) {
allDataArr.push({ type: 'line', name: '', data: [] })
allDataArr[i].name = title[i]
allDataArr[i].data = dataArr[i]
}
console.log(allDataArr)
//打印结果应为:
[
{type:'line',name:'名字1',data:[20,20,20,20,20]},
{type:'line',name:'名字2',data:[40,40,40,40,40]}
]