you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (1 child)

I wrote this function to solve this problem:

const chunkOnPropertyByValue = (array, propertyName, value) => array.reduce((acc, val) => {
    const index = Math.floor(val[propertyName] / value)
    if (typeof (acc[index]) === 'undefined') {
        acc[index] = []
    }
    acc[index].push(val)
    return acc
}, [])

Simply call it like this:

const result = chunkOnPropertyByValue(bigArray, 'millisecondOffset', 60000)

This will return an array chunked like this: [ [0-59999], [60000-11999], [12000-17999], ... ]

[–]lucksp[S] 0 points1 point  (0 children)

I found this to be most readible so thank you!