0

I have this array of points:

0:Point {X: 181708.79357001217, Y: 659243.26713151, rotate: ƒ, move: ƒ, resize: ƒ, …}
1:Point {X: 182534.29357001217, Y: 657010.1837981766, rotate: ƒ, move: ƒ, resize: ƒ, …}
2:Point {X: 186545.37690334552, Y: 660957.76713151, rotate: ƒ, move: ƒ, resize: ƒ, …}
3:Point {X: 181708.79357001217, Y: 659243.26713151,rotate: ƒ, move: ƒ, resize: ƒ, …}

I need to remove the points that have same coordinates.

For example point with index 0 and 3 have the same coordinate. so after I remove it expect to get this array:

0:Point {X: 181708.79357001217, Y: 659243.26713151, rotate: ƒ, move: ƒ, resize: ƒ, …}
1:Point {X: 182534.29357001217, Y: 657010.1837981766, rotate: ƒ, move: ƒ, resize: ƒ, …}
2:Point {X: 186545.37690334552, Y: 660957.76713151, rotate: ƒ, move: ƒ, resize: ƒ, …}

So my question how can I remove repeated points from aary using javascript only?

Michael
  • 13,950
  • 57
  • 145
  • 288
  • you can use reduce method for this – Akhil Aravind May 23 '18 at 07:38
  • this may helps you.. [https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript](https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) – Sree May 23 '18 at 07:39
  • 2
    Possible duplicate of [Remove duplicates from an array of objects in JavaScript](https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) – Muhammad Usman May 23 '18 at 07:40

1 Answers1

1
let array = [
  {X: 181708.79357001217, Y: 659243.26713151},
  {X: 182534.29357001217, Y: 657010.1837981766},
  {X: 186545.37690334552, Y: 660957.76713151},
  {X: 181708.79357001217, Y: 659243.26713151}
]

let answer = [];

array.forEach(arr=> {
    if(!answer.some(an => JSON.stringify(an) === JSON.stringify(arr))){
    answer.push(arr);
  }
});

The above code will loop thru the array,check against uniqueness of answer, if unique then push it into answer

Isaac
  • 12,042
  • 16
  • 52
  • 116