Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

javascript - 使用HTTP请求添加到JSON中的嵌套数组(Add to a nested array in JSON using HTTP Request)

I am using JSON server to create a simple API.

(我正在使用JSON服务器创建一个简单的API。)

I would like to update a nested array in this case the videos array in the playlist with id 'default':

(我想在这种情况下更新播放列表中ID为'default'的视频数组的嵌套数组:)

"playlists": [
   {
      "id": "default",
      "videos": [
        {
          "id": 123
        },
        {
          "id": 456
        }
      ]
    }
 ]

My understanding is that the PATCH specification defines the following operations:

(我的理解是PATCH规范定义了以下操作:)

Add - Adds a value to an object or inserts it into an array

(添加-向对象添加值或将其插入数组)

Following this example :

(下面的例子 :)

{ "op": "add", "path": "/biscuits/1", "value": { "name": "Ginger Nut" } }

My request looks like this:

(我的要求看起来像这样:)

axios.patch('http://localhost:3000/playlists/default', {
  op: 'add',
  path: '/videos',
  value: {"id":789}
});

However this request simply adds the values to the 'default' playlist, without modifying the 'videos' array:

(但是,此请求只是将值添加到“默认”播放列表中,而无需修改“视频”数组:)

"playlists": [{
  "id": "default",
  "videos": [
    {
      "id": 123
    },
    {
      "id": 456
    }
  ],
  "op": "add",
  "path": "/videos",
  "value": "test"
}]

My question is whether there is an easy way to update a nested array using a PATCH request with json-server?

(我的问题是,是否有一种简单的方法可以通过使用带有json-server的PATCH请求来更新嵌套数组?)

  ask by ogot translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...