I have a dataset that has been simplified:
var data = [{
size: 400
}, {
size: 500
}, {
size: 800
}, {
size: 400
}, {
size: 400
} {
size: 300
}, {
size: 300
}, {
size: 800
}];
var windowWidth = 800;
How can I use lodash to create a new array based on how many sizes would fit into the window width. The desired final dataset will be structured like this
var newArray = [{
group: [0],
size: 400
}, {
group: [1],
size: 500
}, {
group: [2],
size: 800
}, {
group: [3, 4],
size: 800
}, {
group: [5,6],
size: 600
}, {
group: [7],
size: 800,
}
The key group
represents the indexes of data[n]
where the size is less than the window width.
The key size
shows the accumulated size of the group.
Note: data[n]
increments and does not loop back to data[0] indefinitely.
Does data[n]
fit inside the window?
If yes, push to group; move to data[n + 1].size
; check condition again
If no, create a new group(); go to data[n + 1].size; check condition