我有两个字典列表: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

最佳答案

对于内循环和外循环,使用相同的变量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)
      }
    }
  }

09-25 17:43