我正在使用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)和动态(.e.g:speed)数据点。
答案 0 :(得分:0)
您可以使用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);