I have an array in the format coming out of d3js treemap using the node.path(target) on every node
var arr = [[{data: A},{data: B},{data: C}],
[{data: C},{data: B},{data: A}],
[{data: B},{data: D},{data: A}],
[{data: A},{data: D},{data: B}]
]
I would like to filter the array to remove the reverse paths i.e [{data: C},{data: B},{data: A}] is the reverse of [{data: A},{data: B},{data: C}] so only [{data: A},{data: B},{data: C}] should be kept.
Thus final array should look like:
var finalarr = [[{data: A},{data: B},{data: C}],
[{data: B},{data: D},{data: A}],
]
I have tried doing a simple reverse comparison.
function removereversepaths(arr){
var cleanpaths = [];
arr.forEach((a) => {
var arev = [...a].reverse();
if (!cleanpaths.includes(arev)){
cleanpaths.push(a);
}
});
}
I am wondering if I can write a filter function to perform this but for some reason I am drawing a blank on how to check the new array in the filter function for the reverse array. Any help/direction is appreciated. Thanks in advance.
{data: X}should help us provide an answer.[{data: A, value: '123'},{data: B, value: '213'},{data: C, value: '530'}]for example. I simplified it because I only need to filter by thedatafield.