我被卡住了。我想使用json中的数据,并使用相同的数据编辑此json :(图像最好是说明性的)
我的JSON文件如下所示:

[{
"method": "GET",
"path": "/",
"aliases": "",
"name": "rootPath",
"handler": "generatedApps/avion01/actions.HomeHandler"
},
{
"method": "GET",
"path": "/avions/",
"aliases": "",
"name": "avionsPath",
"handler": "generatedApps/avion01/actions.AvionsResource.List"
},
{
"method": "GET",
"path": "/notifications/",
"aliases": "",
"name": "notificationsPath",
"handler": "generatedApps/avion01/actions.NotificationsResource.List"
},
{
"method": "POST",
"path": "/notifications/",
"aliases": "",
"name": "notificationsPath",
"handler":
"generatedApps/avion01/actions.NotificationsResource.Create"
}]


并且我想使用“路径”(avions或通知)中的参数,并创建一个名为“ ressourceName”的数据:“(如果path == avions或path ==通知)”

看起来应该像这样:

[{
"method": "GET",
"path": "/",
"aliases": "",
"name": "rootPath",
"handler": "generatedApps/avion01/actions.HomeHandler"
},
{
"ressourceName": "avions",
"method": "GET",
"path": "/avions/",
"aliases": "",
"name": "avionsPath",
"handler": "generatedApps/avion01/actions.AvionsResource.List"
},
{
"ressourceName": "notifications",
"method": "GET",
"path": "/notifications/",
"aliases": "",
"name": "notificationsPath",
"handler": "generatedApps/avion01/actions.NotificationsResource.List"
},
{
"ressourceName": "notifications",
"method": "POST",
"path": "/notifications/",
"aliases": "",
"name": "notificationsPath",
"handler":
"generatedApps/avion01/actions.NotificationsResource.Create"
}]

最佳答案

您必须遍历项目并通过比较路径来更新项目:

items.forEach(item => {
  if(item.path === '/avions/') {
    item.ressourceName = 'avions':
  } else if(item.path === '/notifications/') {
    item.ressourceName = 'notifications':
  }
})

09-20 21:35