I have been struggling with certain issues and would like to find the most effective solution. Despite using map and reduce, I have not been able to achieve the desired results. Any help would be greatly appreciated.
Consider the following INPUT Array structure:
[
{ 'id': 1, 'question_name': "What is your name?", 'question_value': "Jack"},
{ 'id': 2, 'question_name': "What is your hobby?", 'question_value': "Rugby"},
{ 'id': 3, 'question_name': "What is your name?", 'question_value': "Peter"},
{ 'id': 4, 'question_name': "What is your hobby?", 'question_value': "Tennis"}
]
Your task is to create a function that transforms the Array into the desired OUTPUT object:
{
"What is your name?": [{"id": 1, "value": "Jack" }, {"id": 3, "value": "Peter" }],
"What is your hobby?": [{"id": 2, "value": "Rugby"}, {"id": 4, "value": "Tennis"}]
}
Here is a sample code snippet that already exists:
const formatter = (o) => {
const newObject = Object.keys(o).reduce(elm =>
({
"What is your name?": [{'id': elm, 'value': elm}],
"What is your hobby" : [{'id': elm, 'value': elm}],
})
)
return newObject