javascript - JSON 补丁 rfc6902 : Sequential operation and index

标签 javascript json json-patch

在JSON patch中,如何使用索引对同一个数组进行后续操作。例如,考虑

 var source = { colors: ['Red', 'Green', 'Blue'] };
 var target = { colors: [] }; 

补丁文档(操作)

[{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/1"},
{"op":"remove","path":"/colors/2"}]

如果我考虑源的索引,上面的索引是正确的。但是,当我按顺序应用它时,索引不正确。也就是说,如果我删除第 0 个和第 1 个索引,则索引 2 处没有元素。

我可以想出几种方法来处理这个问题。要么将所有删除操作分组在一个数组上,然后保留一个临时结构以在删除期间保存/操作索引中的更改。或者,保持索引相对于可变值

[{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/0"}]

如果操作被认为是资源序列的突变,这是有意义的。

有没有这方面的标准。我在规范中看不到任何相关信息。 A.4. Removing an Array Element

最佳答案

Is there any standard on this

section about the evaluation of the operations似乎很清楚:

Evaluation of a JSON Patch document begins against a target JSON
document.  Operations are applied sequentially in the order they
appear in the array.  Each operation in the sequence is applied to
the target document; the resulting document becomes the target of the
next operation.

关于javascript - JSON 补丁 rfc6902 : Sequential operation and index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23049579/

相关文章:

javascript - 关于 jQuery 脚本的简单问题

ruby-on-rails - 将嵌套数组转换为 JSON

javascript - 在自定义指令上转义 HTML

javascript - 如何从 Mathquill Matheditor 输出为 LaTeX?

android - 如何在android中传递json数组在谷歌地图上绘制折线?

html - 提高 R 代码有效性的技巧

c# - 使用带有 json 补丁的 ASP.NET Core 更新嵌套元素

json - 如何使用 json diff 合并合并 json 子对象并合并?

c# - JSON 补丁更新嵌套对象

javascript - 函数在我的案例中无法执行