首页 > 解决方案 > 在javascript中循环数据

问题描述

我正在尝试在图表中显示数据。我从我的控制器传递的数据如下。

0: {label: "500", backgroundColor: "rgb(102,205,170,0.5)", borderColor: "rgb(102,205,170,0.5)", data: "13,49,2,2"}
1: {label: "1000", backgroundColor: "rgb(0,191,255,0.5)", borderColor: "rgb(0,191,255,0.5)", data: "40,2,41,4"}
2: {label: "1500", backgroundColor: "rgb(112,128,144,0.5)", borderColor: "rgb(112,128,144,0.5)", data: "3,41,0,8"}
3: {label: "2000", backgroundColor: "rgb(220,20,60,0.5)", borderColor: "rgb(220,20,60,0.5)", data: "28,30,40,43"}
4: {label: "2500", backgroundColor: "rgb(240,230,140,0.5)", borderColor: "rgb(240,230,140,0.5)", data: "0,6,20,14"}
5: {label: "3000", backgroundColor: "rgb(0,191,255,0.5)", borderColor: "rgb(0,191,255,0.5)", data: "31,26,31,32"}

现在我想遍历数据所以结果就像

label: '500',
backgroundColor: rgb(102,205,170,0.5),
borderColor: rgb(102,205,170,0.5),
data: [
    26,2,41,13
],
...

我尝试过的如下

for (const [key, value] of Object.entries(dataChart)) {
     console.log(key.borderColor);
}

在这里我只是想显示borderColor,但它说'未定义'

任何帮助将不胜感激。

标签: javascriptloops

解决方案


你需要做的是value.borderColor而不是key.borderColor


推荐阅读