比较对象并拉出深差异 [英] Comparing Objects and Pulling Out Deep Diff

查看:64
本文介绍了比较对象并拉出深差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Node / MongoDB后端中,我正在做一些后期处理,以在记录中发生某些更改时生成注释。因此,第一步是获取记录的个更新版本。然后,我想将两者进行比较,以识别更改的记录部分。我下面的解决方案有效-但它返回包含更改的根级别属性。我需要比这更精细的东西-因为我有一个数组-历史,嵌入在根级别元素中,还有一个数组-服务。



因此,以下面的两个对象为例,有一种方法可以将obj2的 history数组中的最后一个元素拉出来-因为在这种情况下,这就是更改的地方,因此我需要输入



顺便说一句,如果有Underscore或Lodash处理方式,我也完全可以接受。 / p>

这是我现在拥有的代码:



  const obj = {_id:1,服务:[{_id:23,服务: serviceOne,历史记录:[{_id:33,阶段: stageOne}]},{_id:24,服务: serviceTw o,历史记录:[{{_id:44,阶段: stageOne}}},]}; const obj2 = {_id:1,服务:[{_id:23,服务: serviceOne,历史记录:[{_id :33,阶段: stageOne}}},{_id:24,服务: serviceTwo,历史记录:[{_id:45,阶段: stageTwo},{_id:44,阶段: stageOne}} },]};让diff = Object.keys(obj).reduce((diff,key)=> {if((obj [key] === obj2 [key])return diff return {... diff,[key]:obj2 [key]}},{})console.log('diff:',diff);  

解决方案

您可以使用破坏性分配

  let {services:[,{history:[... data]}]} = obj2; 

console.log(data.pop());


In my Node/MongoDB back-end I am doing some post-processing to generate notes when something changes in a record. So the first step is to get a pre and post update version of the record. Then I want to compare the two to identify the part of the record that changed. The solution I have below works - but it returns the root level property containing the change. I need something more granular than this - as I have an array -- "history", embedded within the root level element, also an array -- "services".

So, taking for example my two objects below, is there a way I can just pull out the the last element in the "history" array for obj2 -- since, in this case, that's what's changed, and that's what I need to key in on for then generating my note?

By the way, if there is an Underscore or Lodash way to handle this, I'm completely open to that as well.

Here's the code I have now:

const obj = {
  _id: 1,
  services: [
    {
      _id: 23,
      service: "serviceOne",
      history: [
        {
          _id: 33,
          stage: "stageOne"
        }
      ]
    },
        {
      _id: 24,
      service: "serviceTwo",
      history: [
        {
          _id: 44,
          stage: "stageOne"
        }
      ]
    },
  ]
};

const obj2 = {
  _id: 1,
  services: [
    {
      _id: 23,
      service: "serviceOne",
      history: [
        {
          _id: 33,
          stage: "stageOne"
        }
      ]
    },
        {
      _id: 24,
      service: "serviceTwo",
      history: [
        {
          _id: 45,
          stage: "stageTwo"
        },
        {
          _id: 44,
          stage: "stageOne"
        }
      ]
    },
  ]
};

let diff = Object.keys(obj).reduce((diff, key) => {
  if (obj[key] === obj2[key]) return diff
  return {
    ...diff,
    [key]: obj2[key]
  }
}, {})

console.log('diff: ', diff);

解决方案

You can use destructuring assignment

let {services:[,{history:[...data]}]} = obj2;

console.log(data.pop());

这篇关于比较对象并拉出深差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆