0
重複する既存範囲を日付/番号範囲のリストに分割するアルゴリズムを実装しました。スプリットオーバーラップ範囲
アルゴリズムは動作していますが、アルゴリズムの最後のループを回避できるかどうかは疑問でした。
可能でしょうか?
0-100(赤) 90~150(緑色) 90~150(青) 140から300(イエロー) 170から240(黒) 350から530(オレンジ) 50-500(銀) 50-60(ピンク)
0~49(赤) 50-60(赤、銀、ピンク) 61から89(赤、銀) 90-100(赤、緑、青、銀) 101-139(緑、青、銀) 140-150(緑、青、黄、銀) 151-169(黄色、銀) 170-240 (イエロー、ブラック、銀) 241から300(黄色、銀) 301から349(銀) 350~500(オレンジ、銀) 501から530(オレンジ)
JavaScriptコード:
function splitRanges(original_intervals) {
for (var to = [], from = [], n, i = original_intervals.length; i--;) {
if (to.indexOf(n = original_intervals[i].to) < 0)
to.push(n);
if (from.indexOf(n = original_intervals[i].from) < 0)
from.push(n);
}
to.sort(function(a, b) {
return a - b;
});
from.sort(function(a, b) {
return a - b;
});
var intervals = [];
while (to.length) {
var sFrom = from.shift();
var sTo = 0;
if (from.length == 0) {
sTo = (from.push((n = to.shift()) + 1), n);
} else {
if (from[0] > to[0]) {
while (to[0] < from[0]) {
from.unshift(to[0] + 1);
to.shift();
}
sTo = from[0] - 1;
} else {
sTo = from[0] - 1;
}
}
intervals.push({
from: sFrom,
to: sTo,
colors: []
});
}
/***********************Loop that i want remove*/
intervals.forEach(function(item, index) {
original_intervals.forEach(function(item1, index1) {
if ((item.from >= item1.from && item.from <= item1.to) || (item.to >= item1.from && item.to <= item1.to))
item.colors.push(item1.color);
});
});
return intervals;
}
var r1 = [{
id: 1,
from: 0,
to: 100,
\t color:'red'
}, {
id: 2,
from: 90,
to: 150,
\t color:'green'
}, {
id: 3,
from: 90,
to: 150,
\t color:'blue'
}, {
id: 4,
from: 140,
to: 300,
\t color:'yellow'
}, {
id: 5,
from: 170,
to: 240,
\t color:'black'
}, {
id: 6,
from: 350,
to: 530,
\t color:'orange'
}, {
id: 7,
from: 50,
to: 500,
\t color:'silver'
}
, {
id: 8,
from: 50,
to: 60,
\t color:'pink'
}
];
console.log(splitRanges(r1));
テキスト形式でデータを供給してください。 –