Уничтожить вложенный массив объектов javascript - PullRequest
1 голос
/ 29 марта 2020

Ответы - это массив объектов, и мне нужно отфильтровать, чтобы получить желаемый результат.

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"
     }

Ценю любую помощь

Ответы [ 3 ]

0 голосов
/ 29 марта 2020

Вы могли бы быть очень откровенным об этом, если хотите. Это работает, очень просто читать и понимать, и не занимает много строк кода.

let values = [];

for (let ans of answers) {
    let item = {};
    item["surveyVersion"] =  ans["surveyVersion"]
    item["id"] = ans["surveyVersion"]
    item["surveyId"] = ans["surveyVersion"]
    item["surveyName"] = ans["surveyVersion"]
    item["createdAt"] = ans["surveyVersion"]
    for(let resp of ans.survey) {
        item[resp.data.statement] = resp.data.answer.value;
    }
    values.push(item);
}
0 голосов
/ 29 марта 2020

Используйте map, forEach l oop с деструктурой.

const updated = data =>
  data.map(({ survey, ...item }) => {
    survey.forEach(
      ({
        data: {
          statement,
          answer: { value }
        }
      }) => (item[statement] = value)
    );
    return item;
  });

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"
          }
        }
      }
    ]
  }
];

console.log(updated(answers));
0 голосов
/ 29 марта 2020

Вы можете сделать это с помощью .map(), а затем с помощью .forEach() внутри:

const output = answers.map(child => {
  child.survey.forEach(item => child[item.data.statement] = item.data.answer.value);
  delete child.survey;
  return child;
})

console.log(output);

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"
                    }
                }
            }
        ]

    }
]

const output = answers.map(child => {
  child.survey.forEach(item => child[item.data.statement] = item.data.answer.value);
  delete child.survey;
  return child;
})

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Выход:

[
  {
    "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"
  }
]
...