gpt4 book ai didi

javascript - 如何从时间序列数据构造 'nested' 对象?

转载 作者:太空宇宙 更新时间:2023-11-03 22:24:02 25 4
gpt4 key购买 nike

我正在使用 node.js 和 sql 提取汽车的时间序列数据。

我的查询提取了node.js中的数据,并且它可以作为rawposdata使用:

rawposdata =
[{carID: 1234,
contact: 'john doe',
TimeStamp: 10:00,
Speed = 2.3},
{carID: 1234,
contact: 'john doe',
TimeStamp: 11:00,
Speed = 2.4},
{carID: 1234,
contact: 'john doe',
TimeStamp: 12:00,
Speed = 2.5}
{carID: 9876,
contact: 'bob wills',
TimeStamp: 10:00,
Speed = 1.1},
{carID: 9876,
contact: 'bob wills',
TimeStamp: 11:05,
Speed = 1.1},
{carID: 9876,
contact: 'bob wills',
TimeStamp: 12:00,
Speed = 3.2},
]

我希望它采用以下形式(数组、obj 或其他形式),以便我可以使用它:

mypositiondata = 
[carID: 1234,
contact: 'john doe',
timestamp: { ts0: 10:00,
ts1: 11:00,
ts2: 12:00},
speed: {s0: 2.3
s1: 2.4
s2: 2.5}],
[carID: 9876,
contact: 'bob wills',
timestamp: { ts0: 10:00,
ts1: 11:05,
ts2: 12:00},
speed: {s0: 1.1
s1: 1.1
s2: 3.2}]

我浏览了互联网上的示例,阅读了下划线文档。等等,但似乎我无法解决这个问题。我如何构建这些数据?

我有 50 到 150 辆车需要分类。数据集中有更多静态(例如:carID)和动态(例如:速度)数据点。

最佳答案

您可以使用reduce创建对象并使用Object.values将对象转换为数组。

let mypositiondata = Object.values(rawposdata.reduce((c, v) => {
c[v.carID] = c[v.carID] || {carID: v.carID,contact: v.contact,timestamp: {},speed: {}}
c[v.carID].timestamp["ts" + Object.keys(c[v.carID].timestamp).length] = v.TimeStamp;
c[v.carID].speed["s" + Object.keys(c[v.carID].speed).length] = v.Speed;
return c;
}, {}));

这是一个片段:

let rawposdata = [{carID: 1234,contact: 'john doe',TimeStamp: '10:00',Speed: 2.3},{carID: 1234,contact: 'john doe',TimeStamp: '11:00',Speed: 2.4},{carID: 1234,contact: 'john doe',TimeStamp: '12:00',Speed: 2.5},{carID: 9876,contact: 'bob wills',TimeStamp: '10:00',Speed: 1.1},{carID: 9876,contact: 'bob wills',TimeStamp: '11:05',Speed: 1.1},{carID: 9876,contact: 'bob wills',TimeStamp: '12:00',Speed: 3.2},];

let mypositiondata = Object.values(rawposdata.reduce((c, v) => {
c[v.carID] = c[v.carID] || {carID: v.carID,contact: v.contact,timestamp: {},speed: {}}
c[v.carID].timestamp["ts" + Object.keys(c[v.carID].timestamp).length] = v.TimeStamp;
c[v.carID].speed["s" + Object.keys(c[v.carID].speed).length] = v.Speed;
return c;
}, {}));

console.log(mypositiondata);

<小时/>

还有一种选择是使用数组代替对象来实现时间戳和速度

let rawposdata = [{carID: 1234,contact: 'john doe',TimeStamp: '10:00',Speed: 2.3},{carID: 1234,contact: 'john doe',TimeStamp: '11:00',Speed: 2.4},{carID: 1234,contact: 'john doe',TimeStamp: '12:00',Speed: 2.5},{carID: 9876,contact: 'bob wills',TimeStamp: '10:00',Speed: 1.1},{carID: 9876,contact: 'bob wills',TimeStamp: '11:05',Speed: 1.1},{carID: 9876,contact: 'bob wills',TimeStamp: '12:00',Speed: 3.2},];
let mypositiondata = Object.values(rawposdata.reduce((c,v)=>{
c[ v.carID ] = c[ v.carID ] || {carID : v.carID, contact: v.contact, timestamp : [], speed : []}
c[ v.carID ].timestamp.push( v.TimeStamp );
c[ v.carID ].speed.push( v.Speed );
return c;
},{}));

console.log( mypositiondata );

没有 ES6

var rawposdata = [{carID: 1234,contact: 'john doe',TimeStamp: '10:00',Speed: 2.3},{carID: 1234,contact: 'john doe',TimeStamp: '11:00',Speed: 2.4},{carID: 1234,contact: 'john doe',TimeStamp: '12:00',Speed: 2.5},{carID: 9876,contact: 'bob wills',TimeStamp: '10:00',Speed: 1.1},{carID: 9876,contact: 'bob wills',TimeStamp: '11:05',Speed: 1.1},{carID: 9876,contact: 'bob wills',TimeStamp: '12:00',Speed: 3.2},];

var temp = {};
var mypositiondata = [];

for (var k in rawposdata) {
var v = rawposdata[k];
if (typeof temp[v.carID] === "undefined") temp[v.carID] = {carID: v.carID, contact: v.contact, timestamp: [], speed: []};
temp[v.carID].timestamp.push(v.TimeStamp);
temp[v.carID].speed.push(v.Speed);
}

for (var k in temp) {
var tSpeed = {};
var tTStamp = {};

for (var i in temp[k].speed) tSpeed["s" + i] = temp[k].speed[i];

for (var i in temp[k].timestamp) tTStamp["ts" + i] = temp[k].timestamp[i];

temp[k].speed = tSpeed;
temp[k].timestamp = tTStamp;

mypositiondata.push(temp[k]);
}

console.log(mypositiondata);

关于javascript - 如何从时间序列数据构造 'nested' 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49315477/

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