John Yacoub
John Yacoub

Reputation: 91

remove duplicate objects in an array based on highest value

I am trying to filter an array of objected based on the vch_number is not duplicate & if it is duplicate only return the highest of the lastChecked value.

I have tried filter with indexOf however, indexof only returns the first index only.

Also tried to do it with Set but I couldn't get it to work either.

below is the array

const car = [
  {
    "status": "available",
    "lastChecked": 1,
    "make": "bwm",
    "model": "i8",
    "year": 2000,
    "vch_number": 51511,
    "description": "fully loaded",
    "VehcileStatusReport": {
      "statusId": 1,
      "description": null,
      "createdAt": "2021-03-16T02:02:15.000Z",
      "updatedAt": "2021-03-16T02:02:15.000Z"
    }
  },
  {
    "status": "parked",
    "lastChecked": 2,
    "make": "bwm",
    "model": "i8",
    "year": 2000,
    "vch_number": 51510,
    "description": "fully loaded",
    "VehcileStatusReport": {
      "statusId": 1,
      "description": null,
      "createdAt": "2021-03-16T01:39:48.000Z",
      "updatedAt": "2021-03-16T01:39:48.000Z"
    }
  },
    {
    "status": "service",
    "lastChecked": 3,
    "make": "bwm",
    "model": "i8",
    "year": 2000,
    "vch_number": 51510,
    "description": "fully loaded",
    "VehcileStatusReport": {
      "statusId": 1,
      "description": null,
      "createdAt": "2021-03-16T01:39:48.000Z",
      "updatedAt": "2021-03-16T01:39:48.000Z"
    }
  }
]

I am trying to add to this below function

const filters = ()=>{
const obj = {
}
for (let i of car){
  obj[i.vch_number]= i
}
console.log(obj)
}
filters()

I tried the following & not sure what I am missing

const newVchs = car.reduce((acc, current)=>{
const x = acc.find(item => item.vch_number === current.vch_number && item.lastChecked > current.lastChecked)
if(!x){
  return acc.concat([current])
}else{
  return acc
}

return acc
},[])

console.log(newVchs)

Upvotes: 0

Views: 349

Answers (2)

trincot
trincot

Reputation: 350272

You can create a Map keyed by vch_number. Then iterate the data and let the object associated with that key be the one with the greatest value for lastChecked. Finally, get the values of the Map:

const cars = [{"lastChecked": 1,"vch_number": 51511,}, {"lastChecked": 2,"vch_number": 51510,}, {"lastChecked": 3,"vch_number": 51510,}];

let map = new Map(cars.map(car => [car.vch_number, car]));
for (let car of cars) {
    if (car.lastChecked > map.get(car.vch_number).lastChecked) map.set(car.vch_number, car);
}
let result = [...map.values()];

console.log(result);

(I removed the other properties from the input as only these two are relevant for the code)

Upvotes: 2

pilchard
pilchard

Reputation: 12919

This is a fairly standard groupBy situation, with an added check in the case of a duplicate for the highest lastChecked.

const car = [{ "status": "available", "lastChecked": 1, "make": "bwm", "model": "i8", "year": 2000, "vch_number": 51511, "description": "fully loaded", "VehcileStatusReport": { "statusId": 1, "description": null, "createdAt": "2021-03-16T02:02:15.000Z", "updatedAt": "2021-03-16T02:02:15.000Z" } }, { "status": "parked", "lastChecked": 2, "make": "bwm", "model": "i8", "year": 2000, "vch_number": 51510, "description": "fully loaded", "VehcileStatusReport": { "statusId": 1, "description": null, "createdAt": "2021-03-16T01:39:48.000Z", "updatedAt": "2021-03-16T01:39:48.000Z" } }, { "status": "service", "lastChecked": 3, "make": "bwm", "model": "i8", "year": 2000, "vch_number": 51510, "description": "fully loaded", "VehcileStatusReport": { "statusId": 1, "description": null, "createdAt": "2021-03-16T01:39:48.000Z", "updatedAt": "2021-03-16T01:39:48.000Z" } }];

const result = Object.values(
  car.reduce((acc, car) => {
    acc[car.vch_number] = acc[car.vch_number] || { ...car };

    if (acc[car.vch_number].lastChecked < car.lastChecked) {
      acc[car.vch_number] = { ...car }
    }

    return acc;
  }, {}));

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions