I need to break an array of objects into a 2-dimensional array where 1-dimensional arrays would consist of objects which have their attributes 'for' and 'to' being not overlapping intervals.
Example: Given array arr1 I wish to receive arr2
const arr1 = [
{
from: 0,
to: 2,
},
{
from: 0,
to: 6,
},
{
from: 3,
to: 7,
},
{
from: 2,
to: 4,
},
]
arr2 = [
[
{
from: 0,
to: 2,
},
{
from: 3,
to: 7,
}
],
[
{
from: 0,
to: 6,
}
],
[
{
from: 2,
to: 4,
}
],
]
How it should work: We loop through the arr1. Object1 should be put into the first 1-dimensional array in arr2 by default.
Object2 overlaps with Object1 so it should be put into a separate array in arr2.
Object3 does not overlap Object1 so it should be put into the first 1-dimensional array with Object1
Object4 overlaps with Object1 so it should be put into the second 1-dimensional array to Object3, but it also overlaps with Object3 so it should be put into a separate 1-dimensional array.
I need to find the solution with as less loops as possible)