0

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)

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you trincot! I will have to get familiar with get and set !
I see you wanted the greater lastChecked to prevail, so I changed the < into a >. The principle is the same.
Thank you I noticed that! I am thank you for introducing me to Map objects :)
1

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; }

1 Comment

Thank youPilchard! reduce is always fascinating me! appreciate it, I was trying to achieve it with reduce but I am kinda newbi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.