我在Javascript中有一个对象映射数组,如下所示:
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 }
];
我能够删除具有相同input1和input2的那些(例如上面的第一个条目和第4个条目)的重复数据,但是我似乎找不到并删除具有切换条目的那些(例如第一个条目)和上面的最后一个条目)。我已经搜索过,但找不到类似答案的任何内容。有没有人遇到这个问题并找到了解决方案?
编辑:添加我当前的功能为我如何检查重复项:
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);
}
假设节点的数据类型是字符串或数字。更新的答案:将input1和input2都用作键并将索引存储为值
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);
}
}
对于其他数据类型,您可以始终使用JSON.stringify
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句