Is there a way to manipulate this JSON data?
"forms": [
{
"_id": "Untitled Form",
"title": "Untitled Form",
"answer": [
{
"username": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f555e514c5a514c4b5e510d0b0e0f7f58525e5653115c5052">[email protected]</a>",
"date": "2022-11-02",
"formId": "6361c5aaf7a02c177ebebb27",
"answers": {
"test": [
"New Option"
],
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcd6d3d4d2fcd2dddeddc8d5cfd2dddfd792dfd3d1">[email protected]</a>",
"dropdown": "New Option",
"radio": "3",
"date": "2022-11-01T00:00:00.000Z",
"time": "17:25"
}
},
{
"username": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f4f1f4f8d5e2f0f2faf1f0e3bbf6faf8e6">[email protected]</a>",
"date": "2022-11-03",
"formId": "6361c5aaf7a02c177ebebb27",
"answers": {
"test": [
"New Option"
],
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b18083828083829f8083828083f1d49fd2dedc">[email protected]</a>",
"dropdown": "3",
"radio": "3",
"date": "2022-11-16T00:00:00.000Z",
"time": "09:44"
}
}
]
}
]
I attempted to iterate through it using JavaScript but the returned value is not as expected, and only seems to increment the array values. I tried implementing some logic but couldn't achieve the desired output.
const newData = forms.map((item) => {
var mappedAns = item.answer.map((data) => {
let data_fix = {};
Object.keys(data.answers).forEach((key) => {
data_fix[
key
.replace(/[{()}]/g, "")
.replace(/ :/g, "")
.replace(/ /g, "_")
.toLowerCase()
] = data.answers[key];
});
return { ...data, data_fix };
});
return { ...item, answer: mappedAns };
});