【问题标题】:How to check both objects value are same or not using lodash如何使用 lodash 检查两个对象的值是否相同
【发布时间】: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简化

【问题讨论】:

标签: javascript object lodash


【解决方案1】:

编辑:我弄错了你的问题,抱歉再试一次。

let obj1 = {
  "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"
};

let obj2 = {
  "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"
};

let obj3 = {
  "action": "Accept",
  "destination_port": ""
};
let obj4 = {
  "action": "Accept",
  "destination_port": ""
};


function comp(obj1, obj2) {
  return JSON.stringify(obj1) === JSON.stringify(obj2)
}

console.log(comp(obj1, obj2));
console.log(comp(obj3, obj4));

【讨论】:

  • 您是否运行过这段代码并检查了结果?它不工作
  • 再次查看我的评论看看?
  • @Tasos 再次抱歉。我希望这就是你要找的
  • 现在可以使用,但不适用于它们之间可能具有更多属性的对象
  • Imma 正在寻找更好的答案。 编辑:看起来你找到了答案。
【解决方案2】:

使用_.isEqual() 比较您要检查的两个项目的子集(通过_.pick()):

const isDuplicate = (...objs) => _.isEqual(...objs.map(o => _.pick(
  o,
  ['action', 'destination_port', 'destination_subnet', 'lan_interface', 'protocol', 'source_port', 'source_subnet', 'wan_interface'],
)))

const obj1 = {"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"}

const obj2 = {"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"}

const result = isDuplicate(obj1, obj2)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>

如果第二个对象始终是第一个对象的子集,您可以改用_.isMatch(),但这只会检查obj2 上存在的属性:

const obj1 = {"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"}

const obj2 = {"action":"Accept","destination_port":""}

const result = _.isMatch(obj1, obj2)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多