I am uploading a response from an API that contains forms to its own API, which deconstructs the received object and only returns the object with the necessary properties. The next step to this is to change those values of the keys to something more in line with what I need to integrate into my system.
For example: 'Section_B_Demographic_Details' replace it with a blank space and only 'place_inscription' remains, and so on with all those that repeat that identifier.
Upon receiving the response of the route in which said array is located, I have the following:
{
"data": [
{
"formhub/uuid": "xxxxx",
"Section_B_Demographic_Details/lugar_inscripcion": "xx",
"Section_B_Demographic_Details/nombre_apellido": "xx",
"Section_B_Demographic_Details/tipo_up": "xxx",
"Section_B_Demographic_Details/nombre_up_emprend_negocio": "xxx",
"Section_B_Demographic_Details/comienzo_activ_up": "xxx",
"Section_B_Demographic_Details/calle_up": "xxx",
"Section_B_Demographic_Details/altura_up": "xxx",
"Section_B_Demographic_Details/dato_adic_ubicac_up": "xxx",
"Section_B_Demographic_Details/comuna": "xx",
"Section_B_Demographic_Details/barrio": "xxxx",
"Section_B_Demographic_Details/email_up": "xxx",
"Section_B_Demographic_Details/red_social_up": "xxx"
},
The answer is much larger and has more than 200 records, but for the purpose of solving the doubt, it is enough to show only one. I was trying with several examples, like:
Object.keys(obj).forEach(
(key) => (obj[key] === 'key del json') ? obj[key] = '-' : obj[key]
);
const jsonObj = JSON.stringify(obj,
(key, value) => (value === key del json) ? '-' : value
);
and I cannot find the resolution to replace the match in all the records, it only grabs the first match and leaves the rest as it is.
I thank you in advance for your help and any additional information that may be needed, if it is not clear, I would also appreciate if you inform me so that I can edit the publication.
You can achieve this using the
map
Array method.The idea is to return a new Array with the objects whose keys have been changed. To change the property names you must loop through each object and rename the properties you need.
replace
To replace you can use the String method , and to iterate through the object you can use the loopfor ... in
. For example:I hope this helps you to fix the problem.