Ответы - это массив объектов, и мне нужно отфильтровать, чтобы получить желаемый результат.
const answers = [
{
"surveyVersion": "5",
"id": "550259bd-2164-4f5b-b477-49cd2afc5829",
"surveyId": "123ebeff-9b48-4637-ac63-f0fce9f1abb8",
"surveyName": "Compass",
"createdAt": 1585244952251,
"survey": [
{
"data": {
"statement": "De 0 a 10, qual a chance de você RECOMENDAR o Compass?",
"answer": {
"label": "7",
"value": "7"
}
}
},
{
"data": {
"statement": "De 0 a 10, qual a nota voce dar para a qualidade do atendimento?",
"answer": {
"label": "7",
"value": "6"
}
}
},
{
"data": {
"statement": "De 0 a 10, qual a nota voce da para o interior o Compass?",
"answer": {
"label": "7",
"value": "8"
}
}
}
]
},
{
"surveyVersion": "5",
"id": "c720e777-5085-4493-bed9-c70bc311f47d",
"surveyId": "123ebeff-9b48-4637-ac63-f0fce9f1abb8",
"surveyName": "Compass",
"createdAt": 1585244952251,
"survey": [
{
"data": {
"statement": "De 0 a 10, qual a chance de você RECOMENDAR o Compass?",
"answer": {
"label": "7",
"value": "3"
}
}
},
{
"data": {
"statement": "De 0 a 10, qual a nota voce dar para a qualidade do atendimento?",
"answer": {
"label": "7",
"value": "4"
}
}
},
{
"data": {
"statement": "De 0 a 10, qual a nota voce da para o interior o Compass?",
"answer": {
"label": "7",
"value": "5"
}
}
}
]
}
]
Я должен создать ключ [data.statement] : Значение [data.answer.value] для каждого объекта данных в массиве свойств опроса.
Желаемый результат:
[
{
"surveyVersion": "5",
"id": "550259bd-2164-4f5b-b477-49cd2afc5829",
"surveyId": "123ebeff-9b48-4637-ac63-f0fce9f1abb8",
"surveyName": "Compass",
"createdAt": 1585244952251,
"De 0 a 10, qual a chance de você RECOMENDAR o Compass?":"7" ,
"De 0 a 10, qual a nota voce dar para a qualidade do atendimento?":"6",
"De 0 a 10, qual a nota voce da para o interior o Compass?":"8"
},
{
"surveyVersion": "5",
"id": "c720e777-5085-4493-bed9-c70bc311f47d",
"surveyId": "123ebeff-9b48-4637-ac63-f0fce9f1abb8",
"surveyName": "Compass",
"createdAt": 1585244952251,
"De 0 a 10, qual a chance de você RECOMENDAR o Compass?":"3" ,
"De 0 a 10, qual a nota voce dar para a qualidade do atendimento?":"4",
"De 0 a 10, qual a nota voce da para o interior o Compass?":"5"
}
]
Я пробовал два подхода, один с использованием карты:
let values = answers.map(item => ({
...item,
id: item.id,
survey: item.surveyName,
answer: item.survey.map(function(node) {
return {
[node.data.statement]: node.data.answer.value
};
})
}));
Вывод:
[{
"surveyName": "Compass",
"createdAt": 1585244952251,
"survey": "Compass",
"surveyVersion": "5",
"id": "550259bd-2164-4f5b-b477-49cd2afc5829",
"surveyId": "123ebeff-9b48-4637-ac63-f0fce9f1abb8",
"answer": [{
"De 0 a 10, qual a chance de você RECOMENDAR o Compass?": "7"
},
{
"De 0 a 10, qual a nota voce dar para a qualidade do atendimento?": "8"
},
{
"De 0 a 10, qual a nota voce da para o interior o Compass?": "6"
}
]
},
{
"surveyName": "Compass",
"createdAt": 1585244949217,
"survey": "Compass",
"surveyVersion": "4",
"id": "5156be07-2dc4-47f6-b7c4-1fc5089db0b6",
"surveyId": "123ebeff-9b48-4637-ac63-f0fce9f1abb8",
"answer": [{
"De 0 a 10, qual a chance de você RECOMENDAR o Compass?": "3"
},
{
"De 0 a 10, qual a nota voce dar para a qualidade do atendimento?": "4"
},
{
"De 0 a 10, qual a nota voce da para o interior o Compass?": "5"
}
]
}
]
И еще один подход с использованием деструктуризации, это удивительно, но я не могу понять, как l oop внутри массива опросов.
for (const {id:id,surveyName:surveyName,surveyVersion:surveyVersion,surveyId:surveyId,createdAt:createdAt,survey:[{data:{statement,answer:{value}}}]} of answers)
output = {id,surveyName,surveyVersion,surveyId,createdAt,statement:value}
Вывод:
{
"id": "e233582b-06b7-4257-98a8-8e669a8a341d",
"surveyName": "Compass",
"surveyVersion": "1",
"surveyId": "123ebeff-9b48-4637-ac63-f0fce9f1abb8",
"createdAt": 158524492683,
"statement": "De 0 a 10, qual a chance de você RECOMENDAR o Compass?",
"value": "7"
}
Ценю любую помощь