【发布时间】:2014-05-30 05:14:41
【问题描述】:
我有一个对象数组,它们有一个名为timestamp 和motion 的键。 motion 包含一个值,timestamp 包含一个 unix 时间戳。我想遍历多个对象并找到它们对应的“一天中的时间”时段,然后我想合计一天中给定时间的运动值并将整个事物保存在数组数组中。我希望持续时间是可变的。
假设这些是我的对象;
{
timestamp: 1397160634,
motion: 2,
id: '534771d8c311731e21c75c9f'
},
{
timestamp: 1397160634,
motion: 3,
id: '534771d8c311731e21c75c9f'
}
现在我创建我的结果数组
var sampleDuration = 60; // Min
var minutesInDay = 1440;
var samplesPerDay = minutesInDay/sampleDuration;
var finalResultItem = []
for (var i = 0; i < samplesPerDay; i++) {
var IndividualresultArray = []
IndividualresultArray.push(60*i);
IndividualresultArray.push(0);
finalResultItem.push(IndividualresultArray);
}
我现在有一个数组数组,每个子数组的第一项是一个数字(对应于分钟戳),第二个值是零。
我现在想遍历我的所有对象并根据时间戳中的时间范围增加第二个值 (motion)
_forEach(objects, function (object) {
{
// grab the timestamp
// figure out which minute range it coresponds to
// increment the array value that corresponds to the minute stamp
// rinse and repeat
}
这是我空白的地方,我需要最终结果看起来像这样
[[30, 5],[60, 20],[90, 5],[120, 0] .........]
或者它甚至可以看起来像这样
[[000002400, 5],[000003000, 20],[000003600, 5],[000004200, 0] .........]
其中第一个值是忽略年、月和日的时间戳,只考虑一天中的时间。
我曾考虑以某种方式使用 moment.js,但我不确定如何使用。对这个问题的任何帮助都会很棒。
【问题讨论】:
-
你是不是在创建
finalResultItem时出错了?它不是你的“最终结果”的格式。另外,您能否阐明您想要的第一个值 - “仅考虑一天中的时间”?那是什么价值?分钟?秒? -
是的,psudocode 中有错误,我已修复(我认为)。每个数组的第一个值应该是代表一天中某个时间的数字,除此之外,格式非常随意,我永远不需要超过 5 分钟的分辨率,所以 min 可能是最好的
标签: javascript arrays parsing momentjs