I have an array which contains groups of some adjacent rectangles:
var paths = [
[3,4,6,7],
[8,10],
[13]
];
The rectangles are defined as follows:
var rectangles = [
{id:1,left:[],right:[2]},
{id:2,left:[1],right:[11]},
{id:3,left:[2],right:[4]},
{id:4,left:[3],right:[5]},
{id:5,left:[11],right:[6,12]},
{id:6,left:[5],right:[7]},
{id:7,left:[6],right:[]},
{id:8,left:[],right:[9]},
{id:9,left:[8],right:[10]},
{id:10,left:[9],right:[2]},
{id:11,left:[2],right:[5]},
{id:12,left:[5],right:[]},
{id:13,left:[],right:[9]}
];
From the rectangles definition, i see that rectangles 4 and 6 are not neighbors, because 4 is not right of 6 and 6 is not left of 4. The same for 8 and 10.
Now, i would like to split the paths array to have separate entries for every group of adjacent rectangles, like this:
result = [
[3,4],
[6,7],
[8],
[10],
[13]
];
How can i split the array and create new entries when i find two consecutive id's of two non-adjacent rectangles?
Fiddle with test data: https://jsfiddle.net/4jpy84k4/
EDIT: Final solution thanks to Tobias K.: https://jsfiddle.net/j1of5p4c/4/