JavaScript:动态创建新数组
工作代码: http://jsfiddle.net/sXbRK/
我有各种线段,每个线段都有自己的 ID 。我知道哪些是相互交叉的。
现在,我需要将这些重叠线段的 ID 推入新数组中。
我不关心不互相交叉的线段。
如何将彼此交叉的线段 ID 推送到自己的数组中?
// New arrays of overlapping IDs
e.g. Array A = [1,2,3];
Array B = [7,8,12];
Array C = [14,15];
这是我到目前为止所得到的,不要忘记检查 jsFiddle 上正在进行的代码:
function cross( ls )
{
var len = ls.length;
for(var a=0; a < len - 1; a++)
{
for(var b=a+1; b < len; b++)
{
var A = ls[a],
B = ls[b],
combine = [],
overlappers = [];
if
(
(A.start <= B.start && (A.start + A.end) >= B.start)
||
(A.start <= A.start && (B.start + B.end) >= A.start)
)
{
// Add overlapping elements to arrays
overlappers.push(A.id,B.id);
// Create arrays for line segments that overlap each other
combine[a] = new Array(overlappers);
document.write('Array' + [a] + ' ==> ' + combine[a] + '<br />');
}
}
}
}
// Test data
var lineSegments = [
{id:'1', start:0, end:50},
{id:'2', start:0, end:50},
{id:'3', start:0, end:50},
{id:'4', start:100, end:50},
{id:'5', start:200, end:50},
{id:'6', start:300, end:50},
{id:'7', start:900, end:50},
{id:'8', start:900, end:50},
{id:'9', start:600, end:50},
{id:'10', start:700, end:50},
{id:'11', start:800, end:50},
{id:'12', start:900, end:50},
{id:'13', start:1000, end:50},
{id:'14', start:1100, end:50},
{id:'15', start:1100, end:50}
];
// Execute function
cross(lineSegments);
请帮助提供一些想法。谢谢你!
Working Code: http://jsfiddle.net/sXbRK/
I have various line segments and each has its own ID. I know which ones cross each other.
Now, I need to push the ID of those overlapping line segments into new arrays.
I don't care for the line segments that don't cross each other.
How do I do push the line segment IDs that cross each other into their own array?
// New arrays of overlapping IDs
e.g. Array A = [1,2,3];
Array B = [7,8,12];
Array C = [14,15];
Here's what I have so far and don't forget to check the work-in-progress code on jsFiddle:
function cross( ls )
{
var len = ls.length;
for(var a=0; a < len - 1; a++)
{
for(var b=a+1; b < len; b++)
{
var A = ls[a],
B = ls[b],
combine = [],
overlappers = [];
if
(
(A.start <= B.start && (A.start + A.end) >= B.start)
||
(A.start <= A.start && (B.start + B.end) >= A.start)
)
{
// Add overlapping elements to arrays
overlappers.push(A.id,B.id);
// Create arrays for line segments that overlap each other
combine[a] = new Array(overlappers);
document.write('Array' + [a] + ' ==> ' + combine[a] + '<br />');
}
}
}
}
// Test data
var lineSegments = [
{id:'1', start:0, end:50},
{id:'2', start:0, end:50},
{id:'3', start:0, end:50},
{id:'4', start:100, end:50},
{id:'5', start:200, end:50},
{id:'6', start:300, end:50},
{id:'7', start:900, end:50},
{id:'8', start:900, end:50},
{id:'9', start:600, end:50},
{id:'10', start:700, end:50},
{id:'11', start:800, end:50},
{id:'12', start:900, end:50},
{id:'13', start:1000, end:50},
{id:'14', start:1100, end:50},
{id:'15', start:1100, end:50}
];
// Execute function
cross(lineSegments);
Please help with some ideas. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
Try: