Matriz anidada dentro de un objeto anidado

Tenía un objeto cuya estructura es

const data = {
    "message": "fetch responces successfully",
    "responce": [
        {
            "created_AT": "Mon, 03 Jan 2022 17:39:24 GMT",
            "created_BY": "avinash",
            "dateTime": "Mon, 03 Jan 2022 17:39:24 GMT",
            "deleted_BY": "",
            "flag": 0,
            "project_ID": "infobot1234",
            "responce": {
                "uttence_test_heading": [
                    {
                        "buttons": [
                            {
                                "payload": "/my_localities",
                                "title": "savings"
                            },
                            {
                                "payload": "/my_localities12333qqqwq",
                                "title": "current"
                            },
                            {
                                "payload": "/fruits",
                                "title": "platinum"
                            }
                        ]
                    },
                    {
                        "title": "Test Heading"
                    }
                ]
            },
            "responce_ID": "6bbb20d6-7f71-408a-a78a-bab39a30016f",
            "responce_name": "uttence_test_heading",
            "updated_BY": "",
            "user_ID": "av1234"
        },
        {
            "created_AT": "Tue, 04 Jan 2022 17:49:36 GMT",
            "created_BY": "avinash",
            "dateTime": "Tue, 04 Jan 2022 17:49:36 GMT",
            "deleted_BY": "",
            "flag": 0,
            "project_ID": "infobot1234",
            "responce": {
                "utter_content": [
                    {
                        "text": "text_title for buttonqwqwq"
                    }
                ]
            },
            "responce_ID": "81d699ee-3e78-4356-b703-af095d91e36b",
            "responce_name": "utter_txt1234",
            "updated_BY": "",
            "user_ID": "av1234"
        },
        {
            "created_AT": "Thu, 13 Jan 2022 18:06:39 GMT",
            "created_BY": "avinash",
            "dateTime": "Thu, 13 Jan 2022 18:06:39 GMT",
            "deleted_BY": "",
            "flag": 0,
            "project_ID": "infobot1234",
            "responce": {
                "uttence_text_heading": [
                    {
                        "buttons": [
                            {
                                "payload": "/my_localities",
                                "title": "savings"
                            },
                            {
                                "payload": "/my_localities12333qqqwq",
                                "title": "current"
                            },
                            {
                                "payload": "/test",
                                "title": "premium"
                            }
                        ]
                    },
                    {
                        "title": "Text Heading"
                    }
                ]
            },
            "responce_ID": "bb6b0005-bbd4-49a1-8b25-58e0768800a1",
            "responce_name": "uttence_text_heading",
            "updated_BY": "",
            "user_ID": "av1234"
        },
        {
            "created_AT": "Thu, 13 Jan 2022 20:13:54 GMT",
            "created_BY": "avinash",
            "dateTime": "Thu, 13 Jan 2022 20:13:54 GMT",
            "deleted_BY": "",
            "flag": 0,
            "project_ID": "infobot1234",
            "responce": {
                "uttence_heading_test": [
                    {
                        "buttons": [
                            {
                                "payload": "/my_localities",
                                "title": "savings"
                            },
                            {
                                "payload": "/fruits",
                                "title": "current"
                            },
                            {
                                "payload": "/undefined",
                                "title": "premium"
                            }
                        ]
                    },
                    {
                        "title": "heading test"
                    }
                ]
            },
            "responce_ID": "7aeb2a42-a5f8-464d-832d-47cee4cfdb38",
            "responce_name": "uttence_heading_test",
            "updated_BY": "",
            "user_ID": "av1234"
        }
    ],
    "status_code": 0
}

Pude extraer debajo de la matriz de objetos usando data.responce.map(responce => responce.responce)

[{
  uttence_test_heading: [{
  buttons: [{
  payload: "/my_localities",
  title: "savings"
}, {
  payload: "/my_localities12333qqqwq",
  title: "current"
}, {
  payload: "/fruits",
  title: "platinum"
}]
}, {
  title: "Test Heading"
}]
}, {
  utter_content: [{
  text: "text_title for buttonqwqwq"
}]
}, {
  uttence_text_heading: [{
  buttons: [{
  payload: "/my_localities",
  title: "savings"
}, {
  payload: "/my_localities12333qqqwq",
  title: "current"
}, {
  payload: "/test",
  title: "premium"
}]
}, {
  title: "Text Heading"
}]
}, {
  uttence_heading_test: [{
  buttons: [{
  payload: "/my_localities",
  title: "savings"
}, {
  payload: "/fruits",
  title: "current"
}, {
  payload: "/undefined",
  title: "premium"
}]
}, {
  title: "heading test"
}]
}]

Ahora quiero construir una matriz de objetos cuya estructura es algo como a continuación

Este es mi resultado esperado, pero estoy atascado ya que uttence_name es una clave y es diferente para cada objeto, por lo que alguien podría guiarme sobre cómo puedo lograr el resultado esperado. Por favor ayuda !

[{uttence_name: ' uttence_test_heading',buttons: ['savings','current','platinum'],text: '',responce_ID:'6bbb20d6-7f71-408a-a78a-bab39a30016f'},
{uttence_name: 'utter_content',buttons: '',text: 'text_title for buttonqwqwq',responce_ID:'81d699ee-3e78-4356-b703-af095d91e36b'},
{uttence_name:'uttence_text_heading',buttons:['savings','current','premium'],text: '',responce_ID:'bb6b0005-bbd4-49a1-8b25-58e0768800a1'}]
Answer

Puedes encadenar un segundo mapy obtener el resultado deseado. Pero mi respuesta asume que las matrices solo tienen un solo elemento.

Además, no entendí muy bien qué querías hacer con el buttons, así que los mantuve como una matriz.

Puede ejecutar el fragmento a continuación para ver si está obteniendo el resultado deseado.

const data={message:"fetch responces successfully",responce:[{created_AT:"Mon, 03 Jan 2022 17:39:24 GMT",created_BY:"avinash",dateTime:"Mon, 03 Jan 2022 17:39:24 GMT",deleted_BY:"",flag:0,project_ID:"infobot1234",responce:{uttence_test_heading:[{buttons:[{payload:"/my_localities",title:"savings",},{payload:"/my_localities12333qqqwq",title:"current",},{payload:"/fruits",title:"platinum",},],},{title:"Test Heading",},],},responce_ID:"6bbb20d6-7f71-408a-a78a-bab39a30016f",responce_name:"uttence_test_heading",updated_BY:"",user_ID:"av1234",},{created_AT:"Tue, 04 Jan 2022 17:49:36 GMT",created_BY:"avinash",dateTime:"Tue, 04 Jan 2022 17:49:36 GMT",deleted_BY:"",flag:0,project_ID:"infobot1234",responce:{utter_content:[{text:"text_title for buttonqwqwq",},],},responce_ID:"81d699ee-3e78-4356-b703-af095d91e36b",responce_name:"utter_txt1234",updated_BY:"",user_ID:"av1234",},{created_AT:"Thu, 13 Jan 2022 18:06:39 GMT",created_BY:"avinash",dateTime:"Thu, 13 Jan 2022 18:06:39 GMT",deleted_BY:"",flag:0,project_ID:"infobot1234",responce:{uttence_text_heading:[{buttons:[{payload:"/my_localities",title:"savings",},{payload:"/my_localities12333qqqwq",title:"current",},{payload:"/test",title:"premium",},],},{title:"Text Heading",},],},responce_ID:"bb6b0005-bbd4-49a1-8b25-58e0768800a1",responce_name:"uttence_text_heading",updated_BY:"",user_ID:"av1234",},{created_AT:"Thu, 13 Jan 2022 20:13:54 GMT",created_BY:"avinash",dateTime:"Thu, 13 Jan 2022 20:13:54 GMT",deleted_BY:"",flag:0,project_ID:"infobot1234",responce:{uttence_heading_test:[{buttons:[{payload:"/my_localities",title:"savings",},{payload:"/fruits",title:"current",},{payload:"/undefined",title:"premium",},],},{title:"heading test",},],},responce_ID:"7aeb2a42-a5f8-464d-832d-47cee4cfdb38",responce_name:"uttence_heading_test",updated_BY:"",user_ID:"av1234",},],status_code:0,}

const result = data.responce
  .map((res) => ({...res.responce, responce_ID: res.responce_ID }))
  .map((obj) => ({
    utterance_name: Object.keys(obj)[0],
    text: Object.values(obj)[0][0].text || "",
    buttons: Object.values(obj)[0][0].buttons?.map((btn) => btn.title) || "",
    responce_ID: obj.responce_ID
  }));

console.log(result);

Aquí está el resultado requerido basado en los datos que ha proporcionado. En el código, en el mejor de los casos, verifica si tiene un objeto que contiene texto o no y luego agrega los datos en consecuencia . También tiene response_ID

const data={message:"fetch responces successfully",responce:[{created_AT:"Mon, 03 Jan 2022 17:39:24 GMT",created_BY:"avinash",dateTime:"Mon, 03 Jan 2022 17:39:24 GMT",deleted_BY:"",flag:0,project_ID:"infobot1234",responce:{uttence_test_heading:[{buttons:[{payload:"/my_localities",title:"savings",},{payload:"/my_localities12333qqqwq",title:"current",},{payload:"/fruits",title:"platinum",},],},{title:"Test Heading",},],},responce_ID:"6bbb20d6-7f71-408a-a78a-bab39a30016f",responce_name:"uttence_test_heading",updated_BY:"",user_ID:"av1234",},{created_AT:"Tue, 04 Jan 2022 17:49:36 GMT",created_BY:"avinash",dateTime:"Tue, 04 Jan 2022 17:49:36 GMT",deleted_BY:"",flag:0,project_ID:"infobot1234",responce:{utter_content:[{text:"text_title for buttonqwqwq",},],},responce_ID:"81d699ee-3e78-4356-b703-af095d91e36b",responce_name:"utter_txt1234",updated_BY:"",user_ID:"av1234",},{created_AT:"Thu, 13 Jan 2022 18:06:39 GMT",created_BY:"avinash",dateTime:"Thu, 13 Jan 2022 18:06:39 GMT",deleted_BY:"",flag:0,project_ID:"infobot1234",responce:{uttence_text_heading:[{buttons:[{payload:"/my_localities",title:"savings",},{payload:"/my_localities12333qqqwq",title:"current",},{payload:"/test",title:"premium",},],},{title:"Text Heading",},],},responce_ID:"bb6b0005-bbd4-49a1-8b25-58e0768800a1",responce_name:"uttence_text_heading",updated_BY:"",user_ID:"av1234",},{created_AT:"Thu, 13 Jan 2022 20:13:54 GMT",created_BY:"avinash",dateTime:"Thu, 13 Jan 2022 20:13:54 GMT",deleted_BY:"",flag:0,project_ID:"infobot1234",responce:{uttence_heading_test:[{buttons:[{payload:"/my_localities",title:"savings",},{payload:"/fruits",title:"current",},{payload:"/undefined",title:"premium",},],},{title:"heading test",},],},responce_ID:"7aeb2a42-a5f8-464d-832d-47cee4cfdb38",responce_name:"uttence_heading_test",updated_BY:"",user_ID:"av1234",},],status_code:0,};

let newObject = [];
data.responce.forEach((item) => {
    // We will get each item here now
    // Assuming object of item.responce will come always
    const objectName = Object.keys(item.responce)[0];
    const dataToPush = {
      'responce_ID': item.responce_ID,
      'uttence_name': objectName,
      'buttons': item.responce[objectName][0].buttons ? item.responce[objectName][0].buttons.map(data => data.title) : '',
      'text': item.responce[objectName].find(data => data.hasOwnProperty('text')) ? item.responce[objectName].map(value => value.text)[0] : ''
    };
    // Finally adding the item to the array to give the final output
    newObject.push(dataToPush);
});

console.log(newObject);