【发布时间】:2021-01-17 00:30:23
【问题描述】:
我有两个像下面这样的对象
对象 1:
{
"action": "Accept",
"destination_port": "",
"destination_subnet": "192.168.1.2",
"id": "59-1601004430291",
"lan_interface": "eth0",
"order": 2,
"protocol": "any",
"source_port": "",
"source_subnet": "192.168.0.0/32",
"wan_interface": "wlan0"
}
对象 2:
{
"action": "Accept",
"destination_port": "",
"destination_subnet": "0.0.0.0",
"lan_interface": "eth0",
"protocol": "tcp",
"source_port": "",
"source_subnet": "198.168.43.0",
"wan_interface": "wlan0"
}
我想比较两个对象并检查值是否相同。但是 object1 可能有一些在 object2 中不可用的属性。
我已经完成了如下的实现
const isDuplicate = (o1, o2) => {
if (
o1.action === o2.action &&
o1.destination_port === o2.destination_port &&
o1.destination_subnet === o2.destination_subnet &&
o1.lan_interface === o2.lan_interface &&
o1.protocol === o2.protocol &&
o1.source_port === o2.source_port &&
o1.source_subnet === o2.source_subnet &&
o1.wan_interface === o2.wan_interface
) {
return true;
} else {
return false;
}
};
我想知道它是否可以用lodash简化
【问题讨论】:
-
这能回答你的问题吗? stackoverflow.com/a/23048303/5605822
标签: javascript object lodash