i'm stuck.. i want to use a data from my json, and edit this json with the same data: (images are better then explication) my JSON file looks like that:
[{
"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"
}]
and i want to take the param who are in "path" (avions or notification) and create a data named "ressourceName":"(if path == avions or path == notification)"
That should look like this:
[{
"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"
}]
You have to iterate over items and update the item by comparing the path:
items.forEach(item => {
if(item.path === '/avions/') {
item.ressourceName = 'avions':
} else if(item.path === '/notifications/') {
item.ressourceName = 'notifications':
}
})
Welcome to SO!
For this case, I am assuming that you are holding it as an array of objects in your node code: var resources = JSON.parse('your incoming array')
If so, it is a case of looping through your array and adding the field to each object. Ad the name is the same as the content of your path, you can just strip out the slashes and it should cover you in case any other paths appear
for(var i = 0 ; i < resources.length; i++){
var name = resources[i].path.replace(/\//g, '');
if(name !== ''){
resources[i].ressourceName = name;
}
}
console.log('resources',resources)
resources[i].path
is your path in string format .replace()
replaces occurrences in the path string with another string /\\//g
is a regex to find all forward slashes /
''
is the empty string to replace these found slashes with name !== ''
if the new name is only an empty string (ie path was "/"
) then do not assign the name This modifies your initial resources array object by adding the resourceName
property, but does not change the .path
property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.