-2
let array=  [  [[396.72, 241.07],[396.72, 241.07]], [[1,2]],[[2,1]] ,[[396.72, 241.07],[396.72, 241.07]], [[3,2],[9,1]] ,  [[2,1]],[[1,2]]   ];

Unique Output: [  [[396.72, 241.07],[396.72, 241.07]], [[1,2],[2,1]],   [[3,2],[9,1]]  ]

For example, treat [[1,2]],[[2,1]] as a segment [[x1,y1],[x2,y2]] which has Point 1 and Point 2 .The points are interchangeable since if interchanged it will also form the same segment. May I ask how to find the unique of the array?

There is a solution on 2 dimensional array at here for Points or Coordinates, but its not exactly related to segment. Can anyone guide me on this matter :) ?

VLAZ
  • 26,331
  • 9
  • 49
  • 67

1 Answers1

1

You need to use .find and compare the array, if is not duplicate push the array into result like:

let array = [
  [
    [396.72, 241.07],
    [396.72, 241.07]
  ],
  [
    [1, 2]
  ],
  [
    [2, 1]
  ],
  [
    [396.72, 241.07],
    [396.72, 241.07]
  ],
  [
    [3, 2],
    [9, 1]
  ],
  [
    [2, 1]
  ],
  [
    [1, 2]
  ]
];
const result = [];
array.forEach(el => {
  const isDuplicate = result.find(dp => {
    if (el.length !== dp.length)
      return false;
    return (el.length === 2) ?
      el[0][0] === dp[0][0] && el[0][1] === dp[0][1] :
      el[0][0] === dp[0][0];

  });
  if (!isDuplicate) 
    result.push(el);
  
});

console.log(result);
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34