[[8, 12], [17, 22]],
[[5, 11], [14, 18], [20, 23]]
[[8, 11], [17, 18], [20, 22]]
const a = [[8, 12], [17, 22]];
const b = [[5, 11], [14, 18], [20, 23]];
const result = a.reduce((acc, rangeA) => {
return b.reduce((list, rangeB) => {
if (rangeA[0] >= rangeB[1] || rangeA[1] <= rangeB[0]) {
return list;
}
const from = Math.max(rangeA[0], rangeB[0]);
const to = Math.min(rangeA[1], rangeB[1]);
return [...list, [from, to]];
}, acc);
}, []);
const arr1 = [[8, 12], [17, 22]];
const arr2 = [[5, 11], [14, 18], [20, 23]];
const result = arr1.reduce((acc, range1) => {
arr2.forEach((range2) => {
const rangeLength1 = range2[1] - range1[0];
const rangeLength2 = range1[1] - range2[0];
if(rangeLength1 < rangeLength2) {
if(rangeLength1 < 0) {
return;
}
acc.push([range1[0], range2[1]]);
return;
}
if(rangeLength2 < 0) {
return;
}
acc.push([range2[0], range1[1]]);
});
return acc;
}, []);