英文:
Removing multiple elements from an array using JSON patch
问题
Option 2 is the correct syntax for removing "foo"
and "bar"
using one JSON patch with two operations. Here is the translated JSON patch for Option 2:
[
{ "op": "remove", "path": "/list/0" }, // removes "foo"
{ "op": "remove", "path": "/list/1" } // removes "bar" using new index
]
英文:
Say I have an entity that looks like this:
{
"list": ["foo", "qux", "bar"]
}
If I want to remove "foo"
and "bar"
using one JSON patch with two operations. What is the correct syntax?
Option 1
This option expects the indexes to be immutable while the operations are being invoked.
[
{ "op": "remove", "path": "/list/0" }, // removes "foo"
{ "op": "remove", "path": "/list/2" } // removes "bar" using original index
]
Option 2
This option expects the indexes to shift after the first operation has been invoked.
[
{ "op": "remove", "path": "/list/0" }, // removes "foo"
{ "op": "remove", "path": "/list/1" } // removes "bar" using new index
]
Option 3
This option expects unexpected behavior could occur and guards itself.
[
{ "op": "remove", "path": "/list/2" }, // removes "bar" first to ignore shifting indexes
{ "op": "remove", "path": "/list/0" } // removes "foo"
]
Option 3 is the safest option, I'm just mostly curious about what is correct. The RFC for JSON patch isn't clear on this.
答案1
得分: 1
"remove method on array members has implementation-dependent behaviour with relation to indexes, according the JSON Patch specification RFC 6902.":
"数组成员的remove方法在与索引相关的情况下具有依赖于实现的行为,根据JSON Patch规范RFC 6902。"
"As a result, all three of your suggestions are feasible and contingent on how they are implemented. It's important to note that option 1 implies the immutability of the indexes, which is not necessarily the case.":
"因此,您提出的所有三个建议都是可行的,取决于它们的实现方式。重要的是要注意,选项1 暗示了索引的不可变性,这并不一定是情况。"
"Option 2 moves the indexes after the initial operation, which is a typical strategy and presupposes that the indexes are malleable. By eliminating the piece with the higher index first, Option 3 prevents problems with index shifting.":
"选项2 在初始操作之后移动索引,这是一种典型的策略,假设索引是可变的。通过首先消除较高索引的部分,选项3 可以防止索引移动引发的问题。"
"It is advised to select option 3 or 2 if the order of the array's members is not crucial. The safest option is 3, as it prevents any potential index shifting problems. When the order of the remaining components is crucial and you're certain that the index shifting won't create any problems, you should select option 2, which is shorter.":
"如果数组成员的顺序不重要,建议选择选项3或2。最安全的选择是选项3,因为它可以防止任何潜在的索引移动问题。当剩余组件的顺序至关重要且您确定索引移动不会造成问题时,应选择选项2,因为它更短。"
英文:
The remove method on array members has implementation-dependent behaviour with relation to indexes, according the JSON Patch specification RFC 6902.
As a result, all three of your suggestions are feasible and contingent on how they are implemented. It's important to note that option 1 implies the immutability of the indexes, which is not necessarily the case.
Option 2 moves the indexes after the initial operation, which is a typical strategy and presupposes that the indexes are malleable. By eliminating the piece with the higher index first, Option 3 prevents problems with index shifting.
It is advised to select option 3 or 2 if the order of the array's members is not crucial. The safest option is 3, as it prevents any potential index shifting problems. When the order of the remaining components is crucial and you're certain that the index shifting won't create any problems, you should select option 2, which is shorter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论