I want to remove objects from array with the same mac address based on RSSI value, whether one value is greater than the other one.
For example,
values = [{
mac: 'AC233F271F7E',
rssi: '-60',
}, {
mac: 'AC233F271F80',
rssi: '-65',
}, {
mac: 'AC233F271F7E',
rssi: '-55',
}, {
mac: 'AC233F2AFB37',
rssi: '-70',
}]
How can I remove the object based on duplicate mac address, not only that, but also remove the one with smaller RSSI value. So the end result will look like this,
values = [{
mac: 'AC233F271F80',
rssi: '-65',
}, {
mac: 'AC233F271F7E',
rssi: '-55',
}, {
mac: 'AC233F2AFB37',
rssi: '-70',
}]
I have been looking for many solutions on how to remove duplicate objects in StackOverflow based on conditions. However, all of the solutions I had found remove the objects based on the similarity, for example, like this code below, it removes the object based on duplicate place and name. But I want something that can remove duplicate object when one of the RSSI values is greater than the other.
things.thing = things.thing.filter((thing, index, self) =>
index === self.findIndex((t) => (
t.place === thing.place && t.name === thing.name
))
)
I also tried this code but it only removes duplicate object based on mac address only.
var i;
var obj = {};
var len=vm.allbeacons.length
for ( i=0; i < len; i++ ){
obj[vm.allbeacons[i]['mac']] = vm.allbeacons[i];
}
vm.allbeacons= new Array();
for ( var key in obj )
{ vm.allbeacons.push(obj[key]);
}
I expected a code that can remove objects with the same mac address and also based on RSSI value, whether one value is greater than the other one. Thank you very much in advance if you can help me, really appreciate it!