1

I have an array of object like this :

array = [
   {x: 205, y: 205}
   {x: 230, y: 205}
   {x: 255, y: 205}
   {x: 205, y: 205}
   {x: 205, y: 205}
   {x: 205, y: 205}
]

I want check if my array contain the same object once( for example the 3 last objects are the same) so I want just delete them.

If I select or deselect a item , I just want the coordinates of the selected item in the array

    function selectFixture() {
      const item = new PIXI.Graphics();
      // check if place is empty
      if (this.isDown) {
        this.isDown = false;
        console.log(this.isDown)
        item.beginFill(colors[1]);
        item.drawRect(this.positionX, this.positionY, tileH, tileW);
        container.addChild(item);
        console.log(finalArray)
      } else {
        this.isDown = true;
        console.log(this.isDown)
        item.beginFill(colors[2]);
        item.drawRect(this.positionX, this.positionY, tileH, tileW);
        container.addChild(item);
        selectedFixtures.push({
          x: this.positionX,
          y: this.positionY,
        })
        console.log(selectedFixtures)
        var finalArray = {};
        for (var i = 0, len = selectedFixtures.length; i < len; i++)
          finalArray[selectedFixtures[i]['x']] = selectedFixtures[i];
        selectedFixtures = new Array();
        for (var key in finalArray)
          selectedFixtures.push(finalArray[key]);
        console.log(finalArray)
      }
    }

https://codepen.io/sebastiancz/pen/WNQLxaw

How can I do this ?

TheDevGuy
  • 663
  • 2
  • 12
  • 25

3 Answers3

0

You can use the filter function and a small trick in finding the first index of the current item to remove duplicates like the following:

array = [
   {x: 205, y: 205},
   {x: 230, y: 205},
   {x: 255, y: 205},
   {x: 205, y: 205},
   {x: 205, y: 205},
   {x: 205, y: 205}
]

array = array.filter((item, index) => {
  let ind = array.findIndex(obj => obj.x+"$"+obj.y === item.x+"$"+item.y);
  return ind === index;
});
console.log(array);
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
0

Try This

let  array=[  { x: 205, y: 205 },
{ x: 230, y: 205 },
{ x: 255, y: 205 },
{ x: 205, y: 205 },
{ x: 205, y: 205 },
{ x: 205, y: 205 },]

const removeDuplicates =(array)=> {

   let obj = {}
   array.forEach((elem) => {
      obj[elem.x + "-" + elem.y] = 0
   })

   let newArray = []
   console.log(obj)
   array.forEach((elem) => {
      if (obj[elem.x + "-" + elem.y] !== undefined) {
        if (obj[elem.x + "-" + elem.y] == 0) {
            obj[elem.x + "-" + elem.y] = 1
            newArray.push(elem)
        }
    }


    })
    console.log(newArray)

    return newArray 
 }
KCFragrance
  • 148
  • 6
0

One more way to solve this.

let array = [{x: 205, y: 205},{x: 230, y: 205},{x: 255, y: 205},
{x: 205, y: 205},{x: 205, y: 205},{x: 205, y: 205}]

let sortedArray = []

array.forEach(elem => {
  if (!sortedArray.includes(JSON.stringify(elem))) {
      sortedArray.push(JSON.stringify(elem))
  }
})
sortedArray = sortedArray.map(elem => JSON.parse(elem))
console.log(sortedArray)
Dima Vak
  • 599
  • 5
  • 20