我有2个词典列表:namedLabels
[{"id":"2","image":"45-14.png","name":"test2","xMax":"4784","xMin":"4767","yMax":"64","yMin":"57"},{"id":"5","image":"45-14.png","name":"test5","xMax":"4896","xMin":"4852","yMax":"64","yMin":"59"}]
和otherLabels
[{"id":"1","image":"45-14.png","name":"","xMax":"4760","xMin":"4751","yMax":"64","yMin":"59"},{"id":"3","image":"45-14.png","name":"","xMax":"4830","xMin":"4791","yMax":"67","yMin":"57"},{"id":"4","image":"45-14.png","name":"","xMax":"4845","xMin":"4837","yMax":"64","yMin":"57"},{"id":"6","image":"45-14.png","name":"","xMax":"4914","xMin":"4902","yMax":"67","yMin":"57"}]
我想要做的是:如果namedLabels
的正方形完全覆盖了otherLabels
的正方形,就像完全覆盖它一样,我想要id
通讯员otherLabels
元素打印(在控制台中)。
我的代码:
for (i = 0; i < namedLabels.length; i++) {
for (i = 0; i < otherLabels.length; i++) {
if ((otherLabels[i].xMin<namedLabels[i].xMin) && (otherLabels[i].xMax<namedLabels[i].xMax) &&
(otherLabels[i].yMin<namedLabels[i].yMin) && (otherLabels[i].yMax<namedLabels[i].yMax)) {
console.log (otherLabels[i].id)
}
}
}
我在共享剪辑的第3行上遇到的错误:
Uncaught TypeError: Cannot read property 'xMin' of undefined
at HTMLCanvasElement.c.onclick
答案 0 :(得分:3)
您对内部和外部循环使用相同的变量i
。将内部for
循环的变量更改为j
,它可以正常工作:
var namedLabels = [{"id":"2","image":"45-14.png","name":"test2","xMax":"4784","xMin":"4767","yMax":"64","yMin":"57"},{"id":"5","image":"45-14.png","name":"test5","xMax":"4896","xMin":"4852","yMax":"64","yMin":"59"}]
var otherLabels = [{"id":"1","image":"45-14.png","name":"","xMax":"4760","xMin":"4751","yMax":"62","yMin":"50"},{"id":"3","image":"45-14.png","name":"","xMax":"4830","xMin":"4791","yMax":"67","yMin":"57"},{"id":"4","image":"45-14.png","name":"","xMax":"4845","xMin":"4837","yMax":"64","yMin":"57"},{"id":"6","image":"45-14.png","name":"","xMax":"4914","xMin":"4902","yMax":"67","yMin":"57"}]
for (i = 0; i < namedLabels.length; i++) {
for (j = 0; j < otherLabels.length; j++) {
if ((otherLabels[j].xMin<namedLabels[i].xMin) && (otherLabels[j].xMax<namedLabels[i].xMax) &&
(otherLabels[j].yMin<namedLabels[i].yMin) && (otherLabels[j].yMax<namedLabels[i].yMax)) {
console.log (otherLabels[j].id)
}
}
}
&#13;