Question

I have an array of objects that have a keys called timestamp and motion. motion contains a value and timestamp contains a unix timestamp. I want to iterate over a number of the objects and find what "time of day" period they correspond to, I then want to total up the motion values for that given time of day and save the entire thing in an array of arrays. I want the duration to be changeable.

Let's say these are my objects;

 {
    timestamp: 1397160634,
    motion: 2,
    id: '534771d8c311731e21c75c9f'
 },
 {
    timestamp: 1397160634,
    motion: 3,
    id: '534771d8c311731e21c75c9f'
 }

Now I create my results array

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);
}

I now have an array of arrays with each subarray's first item being a number (corresponding to a minute stamp) and the second value being zero.

I would now like to loop through all my objects and increment the second value (motion) based on the time of day range that is in the timestamp

_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
}

this is where I go blank, I need the end result to look something like this

[[30, 5],[60, 20],[90, 5],[120, 0] .........]

or it could even look like this

[[000002400, 5],[000003000, 20],[000003600, 5],[000004200, 0] .........]

where the first value is a timestamp that ignores the year, month, and day, and only considers the time of day.

I have considered using moment.js in some capacity but I'm not sure how. Any help with this problem would be great.

Was it helpful?

Solution

I created a jsFiddle for you. The motion increment logic should look like (I'm using jQuery here but you get the point)

// Loop through and increment motion
$.each(objs, function (idx, obj) {
    var date = new Date(obj.timestamp * 1000); // Convert to milliseconds
    var minutesInDay = date.getUTCHours() * 60 + date.getUTCMinutes(); // Remove UTC for local time!
    var minuteRange = Math.floor(minutesInDay / sampleDuration);
    finalResultItem[minuteRange][1] += obj.motion;
});

EDIT: Removed some discussion after your edit. I also used more generic logic based on sampleDuration.

OTHER TIPS

This should do it:

_forEach(objects, function (object) {
    var date = new Date(objec.timestamp*1000);
    var minuteOfDay = date.getUTCHours()*60+date.getUTCMinutes();
    finalResultItem[minuteOfDay][1] += object.motion;
})

For a variable sample rate, employ a secondOfDay and divide that by your sampleDuration, then floor it to get your array index.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top