【Javascript】jsondiffpatch检测到子节点改变了,父节点会标记为改变吗?

答案是:No

但是你也有办法检查哪些父节点受到改变的影响,具体分析如下:

jsondiffpatch is designed to be very granular and identifies changes at the most specific path possible.

If a sub-node changes, the parent node itself is not explicitly identified as "changed" in the jsondiffpatch delta. The delta will only show the specific change at the path of the sub-node.

Why?

jsondiffpatch aims for minimal, atomic changes. It doesn't generate a "cascading" change report up the object tree. It pinpoints the exact property or array element that was added, removed, or modified.

Example

Let's illustrate this with an example:

javascript 复制代码
const jsondiffpatch = require('jsondiffpatch').create();

const originalJson = {
  "id": 1,
  "name": "Product A",
  "details": {
    "price": 100,
    "currency": "USD",
    "dimensions": {
      "width": 10,
      "height": 20
      // "depth": 5 // uncomment to see another type of change
    }
  },
  "tags": ["electronics", "gadget"]
};

const modifiedJson = {
  "id": 1,
  "name": "Product A",
  "details": {
    "price": 100,
    "currency": "USD",
    "dimensions": {
      "width": 12, // <--- This sub-node changed
      "height": 20
    }
  },
  "tags": ["electronics", "gadget"]
};

const delta = jsondiffpatch.diff(originalJson, modifiedJson);

console.log("Original JSON:\n", JSON.stringify(originalJson, null, 2));
console.log("\nModified JSON:\n", JSON.stringify(modifiedJson, null, 2));
console.log("\nDelta (changes):\n", JSON.stringify(delta, null, 2));

// --- Let's also check if 'details' or 'dimensions' objects are marked as direct changes ---
// (They won't be)
const detailsDelta = delta && delta.details;
const dimensionsDelta = detailsDelta && detailsDelta.dimensions;

console.log("\nIs 'details' itself in the delta (as a modified object)?", !!detailsDelta && Object.keys(detailsDelta).length > 0);
console.log("Is 'dimensions' itself in the delta (as a modified object)?", !!dimensionsDelta && Object.keys(dimensionsDelta).length > 0);

if (delta) {
  // To check if a specific parent was "affected", you'd look for its properties in the delta.
  const detailsAffected = 'details' in delta;
  const dimensionsAffected = 'details' in delta && 'dimensions' in delta.details;
  const widthAffected = 'details' in delta && 'dimensions' in delta.details && 'width' in delta.details.dimensions;

  console.log("\nProgrammatic check:");
  console.log("'details' node path is present in delta:", detailsAffected);
  console.log("'dimensions' node path is present in delta:", dimensionsAffected);
  console.log("'width' node path is present in delta:", widthAffected);
}

Output Explanation:

复制代码
Original JSON:
 {
  "id": 1,
  "name": "Product A",
  "details": {
    "price": 100,
    "currency": "USD",
    "dimensions": {
      "width": 10,
      "height": 20
    }
  },
  "tags": [
    "electronics",
    "gadget"
  ]
}

Modified JSON:
 {
  "id": 1,
  "name": "Product A",
  "details": {
    "price": 100,
    "currency": "USD",
    "dimensions": {
      "width": 12,
      "height": 20
    }
  },
  "tags": [
    "electronics",
    "gadget"
  ]
}

Delta (changes):
 {
  "details": {
    "dimensions": {
      "width": [
        10,
        12
      ]
    }
  }
}

Is 'details' itself in the delta (as a modified object)? true
Is 'dimensions' itself in the delta (as a modified object)? true

Programmatic check:
'details' node path is present in delta: true
'dimensions' node path is present in delta: true
'width' node path is present in delta: true

As you can see from the Delta (changes) output:

  1. The delta object has a details property.
  2. Inside details, it has a dimensions property.
  3. Inside dimensions, it has a width property, which shows the actual change [10, 12].

jsondiffpatch doesn't report details as being replaced by a new details object, nor dimensions as being replaced. It just shows that something inside details.dimensions.width changed.

How to interpret "parent affected":

While the parent itself isn't marked as replaced, its path is present in the delta object if any of its children (or children's children, etc.) have changed.

So, if you want to know if details was "affected," you would check if delta.details exists and has any properties (indicating changes within it). If delta.details.dimensions exists, then dimensions was affected. If delta.details.dimensions.width exists and contains a change array, then width itself changed.

相关推荐
凡人叶枫21 分钟前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
Tony Bai21 分钟前
再见,丑陋的 container/heap!Go 泛型堆 heap/v2 提案解析
开发语言·后端·golang
发现一只大呆瓜29 分钟前
虚拟列表:支持“向上加载”的历史消息(Vue 3 & React 双版本)
前端·javascript·面试
小糯米60142 分钟前
C++顺序表和vector
开发语言·c++·算法
css趣多多1 小时前
ctx 上下文对象控制新增 / 编辑表单显示隐藏的逻辑
前端
froginwe111 小时前
JavaScript 函数调用
开发语言
阔皮大师1 小时前
INote轻量文本编辑器
java·javascript·python·c#
lbb 小魔仙1 小时前
【HarmonyOS实战】React Native 表单实战:自定义 useReactHookForm 高性能验证
javascript·react native·react.js
独望漫天星辰1 小时前
C++ 多态深度解析:从语法规则到底层实现(附实战验证代码)
开发语言·c++
_codemonster1 小时前
Vue的三种使用方式对比
前端·javascript·vue.js