Javascript 合并传感器数据阵列的最佳方法

Javascript 合并传感器数据阵列的最佳方法,javascript,telemetry,sensor-fusion,Javascript,Telemetry,Sensor Fusion,我正在使用AHRS库创建位置数据的传感器融合阵列 根据Go Pro遥测数据,我有加速计、陀螺仪和磁强计数据阵列,每个样本都有一个时间戳,形状如下: { ACCEL: { samples: [{time, data}, ...] }, .... } 我想把它们合并成一个对象 { time: {accel, gyro, magn} ... } 每个时间戳都有3个值 我有点让它和减速器一起工作 const magn = result[1].streams['MAGN

我正在使用AHRS库创建位置数据的传感器融合阵列

根据Go Pro遥测数据,我有加速计、陀螺仪和磁强计数据阵列,每个样本都有一个时间戳,形状如下:

{
  ACCEL: {
     samples: [{time, data}, ...]
  },
   ....
}
我想把它们合并成一个对象

{
  time: {accel, gyro, magn}
  ...
}
每个时间戳都有3个值

我有点让它和减速器一起工作

const magn = result[1].streams['MAGN'].samples.reduce((prev, next) => {
    return {...prev, [next.cts]: {magn: next.value}}
}, {})

const gyro = result[1].streams['GYRO'].samples.reduce((prev, next) => {
    const closest = prev[Object.keys(prev).reverse()?.find(key => key < next.cts) || Object.keys(prev)[0]]
    return {...prev, [next.cts]: {...closest, gyro: next.value}}
}, magn)

const merged = result[1].streams['ACCL'].samples.reduce((prev, next) => {
    const closest = prev[Object.keys(prev).reverse()?.find(key => key < next.cts) || Object.keys(prev)[0]]
    return {...prev, [next.cts]: {...closest, accel: next.value}}
}, gyro)
const magn=result[1]。流['magn']。样本。减少((上一个,下一个)=>{
返回{…prev[next.cts]:{magn:next.value}
}, {})
常数陀螺=结果[1]。流['gyro']。采样。减少((上一个,下一个)=>{
const closest=prev[Object.keys(prev).reverse()?.find(key=>key{
const closest=prev[Object.keys(prev).reverse()?.find(key=>key
但这似乎不是很优雅的代码

有没有更有效的方法来处理它?

typescript

type Input = Record<string, { samples: { time: number, data: any }[] }>

type Output = Record<'number', { [K in keyof Input]: any }>

const inp: Input = { ACCEL: { samples: [{ time: 12, data: '12data' }, { time: 14, data: '14data' }]}, GYRO: { samples: [{ time: 18, data: 'gyro18' }, { time: 12, data: 'gyro12' }] }};

const keyOfInput = Object.keys(inp)

const collectionOfTimes =  [
  ... new Set(
    keyOfInput
      .map(key => inp[key].samples.map(s => s.time))
      .reduce((acc, curr) => [...acc, ...curr], []))
]

const out: Output = collectionOfTimes.reduce((acc, time) => ({ ...acc, [time]: 
  keyOfInput.reduce((acc2, key) => ({...acc2, [key]: inp[key].samples.find(i => i.time === time)?.data || null}), {})
}), {} as Output);

console.log(out);

/*
[LOG]: { "12": { "ACCEL": "12data", "GYRO": "gyro12" }, "14": { "ACCEL": "14data", "GYRO": null }, "18": { "ACCEL": null, "GYRO": "gyro18" } } 
*/
"use strict";
const inp = { ACCEL: { samples: [{ time: 12, data: '12data' }, { time: 14, data: '14data' }] }, GYRO: { samples: [{ time: 18, data: 'gyro18' }, { time: 12, data: 'gyro12' }] } };
const keyOfInput = Object.keys(inp);
const collectionOfTimes = [
    ...new Set(keyOfInput
        .map(key => inp[key].samples.map(s => s.time))
        .reduce((acc, curr) => [...acc, ...curr], []))
];
const out = collectionOfTimes.reduce((acc, time) => (Object.assign(Object.assign({}, acc), { [time]: keyOfInput.reduce((acc2, key) => { var _a; return (Object.assign(Object.assign({}, acc2), { [key]: ((_a = inp[key].samples.find(i => i.time === time)) === null || _a === void 0 ? void 0 : _a.data) || null })); }, {}) })), {});
console.log(out);
/*
[LOG]: { "12": { "ACCEL": "12data", "GYRO": "gyro12" }, "14": { "ACCEL": "14data", "GYRO": null }, "18": { "ACCEL": null, "GYRO": "gyro18" } }
*/