【问题标题】:remove duplicate objects in an array based on highest value根据最大值删除数组中的重复对象
【发布时间】:2021-03-20 21:40:07
【问题描述】:

我正在尝试根据 vch_number 不重复过滤一个对象数组,如果重复,则仅返回 lastChecked 值的最高值。

我试过用 indexOf 过滤,但是 indexof 只返回第一个索引。

也尝试用 Set 来做,但我也无法让它工作。

下面是数组

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 filters = ()=>{
const obj = {
}
for (let i of car){
  obj[i.vch_number]= i
}
console.log(obj)
}
filters()

我尝试了以下但不确定我错过了什么

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)

【问题讨论】:

    标签: javascript arrays object higher-order-functions


    【解决方案1】:

    您可以创建一个以 vch_number 为键的映射。然后迭代数据,让与该键关联的对象成为 lastChecked 值最大的对象。最后,获取 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);

    (我从输入中删除了其他属性,因为只有这两个与代码相关)

    【讨论】:

    • 谢谢 trincot!我必须熟悉 get 和 set !
    • 我知道您希望更大的lastChecked 占上风,所以我将< 更改为>。原理是一样的。
    • 谢谢我注意到了!感谢您向我介绍 Map 对象 :)
    【解决方案2】:

    这是一个相当标准的groupBy 情况,在重复最高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; }

    【讨论】:

    • 谢谢皮查德!减少总是让我着迷!感谢它,我试图通过 reduce 来实现它,但我有点新手
    猜你喜欢
    • 1970-01-01
    • 2016-11-08
    • 2020-06-29
    • 2015-05-13
    • 2019-10-26
    • 2019-02-12
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多