I have a mapping array of objects in Javascript, like so:
Mapping_Array = [
{ input1: node_0, input2: node_1 },
{ input1: node_1, input2: node_2 },
{ input1: node_2, input2: node_3 },
{ input1: node_0, input2: node_1 },
{ input1: node_1, input2: node_0 }
];
I was able to de-duplicate those that have the same input1 and input2 (such as the first entry and the 4th entry above), but I can't seem to find and remove those that have the switched entries (such as the first entry and the last entry above). I've searched but can't find anything on SO that has a similar answer. Has anyone encountered this & found a solution?
EDIT: adding my current function for how I check for duplicates:
function check_mapping_array() {
// De-duplicate the array
jsonObject = Mapping_Array.map(JSON.stringify);
uniqueSet = new Set(jsonObject);
Mapping_Array = Array.from(uniqueSet).map(JSON.parse);
}
Assuming the data type of node is a string or number. Updated answer: use both input1 and input2 as key and store index as a value
let map = new Map();
let arr = [
{ input1: "node_0", input2: "node_1" },
{ input1: "node_1", input2: "node_2" },
{ input1: "node_2", input2: "node_3" },
{ input1: "node_0", input2: "node_1" },
{ input1: "node_1", input2: "node_0" }
];
map.set(arr[0]["input1"] + "|" + arr[0]["input2"], 0);
for(let i=1; i<arr.length;i++){
let temp = map.get(arr[i]["input2"] + "|" + arr[i]["input1"]);
if(temp != undefined){//switch exists
console.log("current index",i);
console.log("prev index", temp);
//now you remove the previous entry as well
}
else{
map.set(arr[i]["input1"] + "|" + arr[i]["input2"], i);
}
}
For other data types you can always use JSON.stringify