根据最大值删除数组中的重复对象
remove duplicate objects in an array based on highest value
我正在尝试根据 vch_number
不重复来过滤对象数组,如果它仅重复 return lastChecked
值中的最高值。
我已经尝试使用 indexOf 进行过滤,但是,indexof 仅 return 仅是第一个索引。
也尝试用 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)
您可以创建由 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);
(我从输入中删除了其他属性,因为只有这两个与代码相关)
这是一个相当标准的 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; }
我正在尝试根据 vch_number
不重复来过滤对象数组,如果它仅重复 return lastChecked
值中的最高值。
我已经尝试使用 indexOf 进行过滤,但是,indexof 仅 return 仅是第一个索引。
也尝试用 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)
您可以创建由 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);
(我从输入中删除了其他属性,因为只有这两个与代码相关)
这是一个相当标准的 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; }