gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:17:44 24 4
gpt4 key购买 nike

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

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

编辑:4 个数组中的每一个都有 9 个 [key, value] 对:

 (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)

关于javascript - 将嵌套 JSON 数组转换为 Chart.js 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60923361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com