I had an object whose structure is
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
}
I was able to extract below array of objects by using 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"
}]
}]
Now I want to construct an array of objects whose structure is something like below
This is my expected result, but I am stuck since the uttence_name is a key and it is different for each object so could someone please guide me through how can I acheive my expected result. Please Help !
[{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'}]
buttonsindented to be an array? Then surround all those strings with square brackets, also''to be consistent, or even better: an empty array. Note that JS has a specific value for the absence of a value, and it is not the empty string.