【发布时间】:2021-03-31 18:17:53
【问题描述】:
我整天都在用头撞墙试图解决这个问题,也在这里搜索过,我还没有找到任何东西。
我从 API 返回的当前对象的结构是这样的,每个设备都有超过 2000 个数据点:
{ "DeviceA": [
{ "device_id": "DeviceA",
"time": "2021-01-23T18:43:14Z",
"temperature": 1.875,
"strength": -14.827574,
"maturity": 105.904513
},
{ "device_id": "ExampleB",
"time": "2021-01-23T18:53:14Z",
"temperature": 1.35,
"strength": -145.64274540827574,
"maturity": 10.904513
},
]
"DeviceB": [
{ "device_id": "DeviceB",
"time": "2021-01-23T18:43:14Z",
"temperature": 1.5,
"strength": -120.64274540827574,
"maturity": 3233.04513
},
{ "device_id": "ExampleB",
"time": "2021-01-23T18:53:14Z",
"temperature": 1.875,
"strength": -17.4274540827574,
"maturity": 10554.268851904513
},
}
以及我想要的方式
[
{
"id": "DeviceA"
"Data": [
{
"x": "time"
"y": "temperature"
}
]
}
{
"id": "DeviceB"
"Data": [
{
"x": "time"
"y": "temperature"
}
]
}
]
我尝试过嵌套映射数组,数以百万计的东西,但我永远无法获得正确的输出。任何帮助将不胜感激!
编辑:
很抱歉,我没有为我正在做的事情包含一些代码
const graphAllTempVTime = () => {
const sensorTempVTimeMapped = Object.entries(sensorDatasFromCast).map(
(items, index) => ({
id: items[0],
data: [
{
x: items[1].time,
y: items[1].temperature,
},
],
})
)
return sensorTempVTimeMapped
}
但是这会产生以下输出:
【问题讨论】:
-
只要确保我有这个权利。所以你的 API 推出的是一个巨大的对象,而不是一个包含对象的数组?
-
是的。顶层是一个对象
标签: javascript arrays javascript-objects