Let's call reversed what {"src":"A", "target":"B"} is to {"src":"B", "target":"A"}. As a first step I want to remove all reversed objects from the array. My code right below is not giving me the expected result.
var reducedArr = [
{"src":"A", "target":"B", "val":"15"},
{"src":"A", "target":"C", "val":"11"},
{"src":"B", "target":"A", "val":"9"},
{"src":"C", "target":"A", "val":"5"},
{"src":"C", "target":"B", "val":"18"},
{"src":"B", "target":"A", "val":"19"}
]
var result= reducedArr
result.forEach(element => {
r = reducedArr.find(e => {return (e.src == element.target && e.target == element.src)});
if(r){
reducedArr = reducedArr.filter(ee => ee !== r)
console.log(reducedArr)
}
});
The proper result should be :
[
{"src":"A", "target":"B", "val":"15"},
{"src":"A", "target":"C", "val":"11"},
{"src":"C", "target":"B", "val":"18"},
]
Once I figure that out, I would like to add a condition where between the object and it's reversed version, I would keep the one that has the highest value for "val". So the ideal final result would be :
[
{"src":"A", "target":"C", "val":"11"},
{"src":"C", "target":"B", "val":"18"},
{"src":"B", "target":"A", "val":"19"}
]