Javascript remove duplicates from Array of Array of Objects

ManuelMB

I want to remove the duplicates from Array of Objects, I tried creating a Set from the Array, using includes and using indexOf, but can get it ok.

class Car{
  constructor(index){
    this.index = index
  }
}

const car1 = new Car(0)
const car2 = new Car(1)
const car3 = new Car(3)

const car4 = new Car(0)
const car5 = new Car(1)
const car6 = new Car(3)

const car7 = new Car(7)
const car8 = new Car(8)
const car9 = new Car(9)


const group1 = [car1, car2, car3]
const group2 = [car4, car5, car6]
const group3 = [car7, car8, car9]

const orders = [group1, group2, group3]

// Using a Set:
const ordersSet = new Set(orders)
console.log(ordersSet)

/*
Set(3) {
  [ Car { index: 0 }, Car { index: 1 }, Car { index: 3 } ],
  [ Car { index: 0 }, Car { index: 1 }, Car { index: 3 } ],
  [ Car { index: 7 }, Car { index: 8 }, Car { index: 9 } ]
}
*/

  // Using includes or using indexOf:

  const uniqueOrders = []

  for (let i = 0; i < orders.length; i++) {
      const newElement =  orders[i]
      if(!uniqueOrders.includes(newElement)){ // Using includes
      //if(uniqueOrders.indexOf(newElement) === -1){ // Using indexOf
          uniqueOrders.push(newElement) 
      }
  }
  console.log(uniqueOrders)

/*
   [
      [ Car { index: 0 }, Car { index: 1 }, Car { index: 3 } ],
      [ Car { index: 0 }, Car { index: 1 }, Car { index: 3 } ],
      [ Car { index: 7 }, Car { index: 8 }, Car { index: 9 } ]
   ]
*/

In my case I would like to get an array with just group1 and group3 or group2 and group3 because group1 is equal to group2:

/*
   [
      [ Car { index: 0 }, Car { index: 1 }, Car { index: 3 } ],
      [ Car { index: 7 }, Car { index: 8 }, Car { index: 9 } ]
   ]
*/
Baldo

You can use the "filter" array method.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

On second thought... this would be of no use in this case. Because you are trying to filter items through multiple arrays instead of in the same array.

But one thing you're able to do here is use a hashmap to map which car indexes you've already seen...

const uniqueOrders = []
let filter = {}

for (let i = 0; i < orders.length; i++) {
  const carList =  orders[i]  // This would get the whole array of the set and not just the car object
  const uniqueCarList = []
  for (let j = 0; j < carList.length; j++) {
    const car =  carList[j]
    
    if(!filter[car.index]){
      filter[car.index] = true
      uniqueCarList.push(car)
    }
  }

  if(uniqueCarList.length){
    uniqueOrders.push(uniqueCarList)
  }
}

I think this should output what you are expecting or at least helps you get there

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

javascript remove duplicates from array of objects

Remove duplicates from an array of objects in JavaScript

JavaScript: Remove duplicates from array of objects

How to remove duplicates objects from array in javascript?

Remove duplicates from Array with Objects

Remove duplicates from array of objects

remove duplicates from array of objects array

Remove duplicates from JavaScript Array of Objects based on Key

From an array of objects, collect and remove duplicates for all attributes in JavaScript

JavaScript - Count and remove duplicates from array of objects ES6

Remove duplicates from javascript objects array based on object property value

How to remove duplicates from the array of array in javascript?

Remove duplicates from Array of objects in Typescript

Remove duplicates from an array of objects in jsonnet

Remove Duplicates from an Array of GeoFire Objects

Remove duplicates from array of objects conditionally

Remove duplicates from Array of Array (nested array of objects)

Remove duplicates from JavaScript array using Python

Remove Duplicates from array of objects but keep empty string value objects

Remove duplicates from an array of array

Remove duplicates from an array of objects and Add new Value (e.g. Quantity) in JavaScript

JavaScript - Array of Objects - Compare & Remove Duplicates ES6

Javascript: Remove duplicates in array of objects using a single property

How can I remove and count duplicates in an array of objects? - Javascript

Javascript remove duplicates from multidimentional array based on first element of array

js remove duplicate from array of objects by values and count duplicates

How to remove duplicates from an array of JSON objects in Nodejs

Remove duplicates objects from array by name and max category

Compare two arrays having objects and remove duplicates from first array