如何过滤出数组中的重复对象?

时间:2019-06-16 15:37:09

标签: javascript html arrays typescript object

我有一个充满三角形的数组。三角形由3个点对象(x和y坐标)组成。从图中可以看出,某些三角形由相同的点组成,但顺序与先前的顺序不同。如何过滤出这些重复项? 注意:我提供了下面无法正常工作的代码。

enter image description here

enter image description here

     let cleanedTriangles = [];

     for (let i = 0; i < triangles.length; i++)
     {
        if (i = 0)
        {
            cleanedTriangles.push(triangles[i])
        }
        else
        {
            for (let j = 0; j < cleanedTriangles.length; j++)
            {
              if (
                 !((triangles[i].p1.x === cleanedTriangles[j].p1.x || triangles[i].p1.x === cleanedTriangles[j].p2.x || triangles[i].p1.x === cleanedTriangles[j].p3.x)
                     &&
                     (triangles[i].p2.x === cleanedTriangles[j].p1.x || triangles[i].p2.x === cleanedTriangles[j].p2.x || triangles[i].p2.x === cleanedTriangles[j].p3.x)
                     &&
                     (triangles[i].p3.x === cleanedTriangles[j].p1.x || triangles[i].p3.x === cleanedTriangles[j].p2.x || triangles[i].p3.x === cleanedTriangles[j].p3.x)
                     &&
                     (triangles[i].p1.y === cleanedTriangles[j].p1.y || triangles[i].p1.y === cleanedTriangles[j].p2.y || triangles[i].p1.y === cleanedTriangles[j].p3.y)
                     &&
                     (triangles[i].p2.y === cleanedTriangles[j].p1.y || triangles[i].p2.y === cleanedTriangles[j].p2.y || triangles[i].p2.y === cleanedTriangles[j].p3.y)
                     &&
                     (triangles[i].p3.y === cleanedTriangles[j].p1.y || triangles[i].p3.y === cleanedTriangles[j].p2.y || triangles[i].p3.y === cleanedTriangles[j].p3.y)
                 )
              )
              {
                 cleanedTriangles.push(triangles[i])
              }
            }

        }
     }


function Point(x, y)
{

    this.x = x || 0;    

    this.y = y || 0;

}


function Triangle(point1, point2, point3)
{
    this.p1 = point1 || new Point(0, 0);    

    this.p2 = point2 || new Point(0, 0);

    this.p3 = point3 || new Point(0, 0);
}

2 个答案:

答案 0 :(得分:3)

您可以获取数组中的点,按<TextView android:text="test" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/widget_text" android:gravity="center" android:background="?android:attr/selectableItemBackgroundBorderless" android:clickable="true" android:focusable="true" android:textColor="#ddd" app:autoSizeTextType="uniform" app:autoSizeMaxTextSize="200dp" app:autoSizeMinTextSize="12dp"/> x升序排序,从坐标中创建一个字符串,并将其用作y的键。然后过滤。

Set
var array = [{ p1: { x: 1, y: 1 }, p2: { x: 3, y: 1 }, p3: { x: 1, y: 4 } }, { p1: { x: 1, y: 1 }, p2: { x: 3, y: 1 }, p3: { x: 1, y: 4 } }, { p1: { x: 1, y: 1 }, p2: { x: 1, y: 4 }, p3: { x: 3, y: 1 } }, { p1: { x: 1, y: 1 }, p2: { x: 3, y: 1 }, p3: { x: 1, y: 4 } }, { p1: { x: 1, y: 1 }, p2: { x: 2, y: 7 }, p3: { x: 1, y: 4 } }, { p1: { x: 2, y: 5 }, p2: { x: 3, y: 1 }, p3: { x: 1, y: 4 } }],
    triangles = new Set,
    result = array.filter(({ p1, p2, p3 }) => {
        var key = JSON.stringify([p1, p2, p3].sort((a, b) => a.x - b.x || a.y - b.y));
        return !triangles.has(key) && triangles.add(key);
    });

console.log(result);
console.log([...triangles]);

答案 1 :(得分:1)

我会做一个非常合乎逻辑的方法:计算三角形的周长。如果两个三角形的周长不同,那么它们就不可能相同,因此它们的顶点(或峰?或英语中的任何东西;)就不能相同。

const triangleArray = [{
    p1: {
      x: 10,
      y: 5
    },
    p2: {
      x: 11,
      y: 5
    },
    p3: {
      x: 10,
      y: 2
    }
  },
  {
    p1: {
      x: 8,
      y: 4
    },
    p2: {
      x: 7,
      y: 5
    },
    p3: {
      x: 10,
      y: 2
    }
  },
  {
    p1: {
      x: 10,
      y: 5
    },
    p2: {
      x: 11,
      y: 5
    },
    p3: {
      x: 10,
      y: 2
    }
  }
]


triangleArray.forEach(triangle => {
  triangle.perimeter = calcPerimeter(triangle.p1, triangle.p2, triangle.p3)
})

console.log(triangleArray)

// Math.hypot() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot
function calcSideLength(p1, p2) {
  return Math.hypot(p1.x - p2.x, p1.y - p2.y)
}

function calcPerimeter(p1, p2, p3) {
  return calcSideLength(p1, p2) + calcSideLength(p2, p3) + calcSideLength(p3, p1)
}

然后我将检查那些具有相同端点的三角形是否具有相同的端点