将嵌套的JSON数组转换为Chart.js输入



我似乎无法正确排列React-Chart.js的数据。我目前将数据放在一个数组中:

[[Array(9)], [Array(9)],[Array(9)], [Array(9)]]

编辑:4个数组中的每一个都有9个[键,值]对在自己的数组中:

(9) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]
(9) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]
(9) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]
(9) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]

在未来,我希望能够添加更多的阵列。

我想使用每个数组作为自己的系列,并使用另一个来源的名称:

{
label: props.name[0],
data: [
aminos[0][0],
aminos[0][1],
aminos[0][2],
aminos[0][3],
aminos[0][4],
aminos[0][5],
aminos[0][6]
etc...
]
},
{
label: props.name[1],
data: [
aminos[1][0],
aminos[1][1],
aminos[1][2],
aminos[1][3],
aminos[1][4],
aminos[1][5],
aminos[1][6],
etc...
]
},
...

希望这是合理的,提前谢谢!

下面的代码片段应该很有帮助,我不确定你的源数组的结构,因此推广了这个

//sample data
var recArray = [ 
[ 
[ 'key1','value1.1'], 
['key2','value2.1'],
['key9','value9.1']
],
[ 
[ 'key1','value1.2'], 
['key2','value2.2'],
['key9','value9.2']
]
];
//result object
lblGroupedRecords = {}
//flattens the nested array
//choose nested level based on your level of nesting
flattenedRecords = recArray.flat(2);
var handle=0;
while(handle<flattenedRecords.length) {
var key = flattenedRecords[handle++];
var value = flattenedRecords[handle++];
(lblGroupedRecords[key] = lblGroupedRecords[key] || []).push(value);
}
console.log(lblGroupedRecords)

相关内容

  • 没有找到相关文章

最新更新