在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/