4

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.

2
  • Did you make a mistake in creating finalResultItem? It's not in the format of your "end result". Also, can you clarify what you want as the first value - "only considers the time of day"? What value is that? Minutes? Seconds? Commented Apr 15, 2014 at 18:27
  • Yes there was a mistake in the psudocode, I fixed (I think). The first value of each array should be a number that represents a time of day, other then that the format is pretty arbitrary, I'm never going to need a resolution of more than 5 min, so min is probably the best Commented Apr 15, 2014 at 18:44

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@user379468 Okay, I edited to take your changes into account and made it more generic. Let me know if this gets you on the right track!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.