मेरे पास एक वस्तु थी जिसकी संरचना है

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
}

मैं 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"
}]
}]

अब मैं उन वस्तुओं की एक सरणी बनाना चाहता हूं जिनकी संरचना नीचे की तरह है

यह मेरा अपेक्षित परिणाम है, लेकिन मैं फंस गया हूं क्योंकि uttence_name एक कुंजी है और यह प्रत्येक वस्तु के लिए अलग है, इसलिए क्या कोई मेरा मार्गदर्शन कर सकता है कि मैं अपना अपेक्षित परिणाम कैसे प्राप्त कर सकता हूं। कृपया सहायता कीजिए !

[{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'}]
0
Sachin 15 जिंदा 2022, 11:38
आपका वांछित आउटपुट मान्य जावास्क्रिप्ट नहीं है। क्या buttons को एक ऐरे के रूप में इंडेंट किया गया है? फिर उन सभी स्ट्रिंग्स को वर्गाकार कोष्ठकों से घेरें, '' सुसंगत होने के लिए, या इससे भी बेहतर: एक खाली सरणी। ध्यान दें कि किसी मान की अनुपस्थिति के लिए JS का एक विशिष्ट मान है, और यह खाली स्ट्रिंग नहीं है।
 – 
trincot
15 जिंदा 2022, 12:04

3 जवाब

सबसे बढ़िया उत्तर

आप एक सेकंड map को चेन कर सकते हैं और वांछित परिणाम प्राप्त कर सकते हैं। लेकिन मेरा जवाब मानता है कि सरणी में केवल एक ही वस्तु है।

साथ ही, मुझे समझ में नहीं आया कि आप buttons के साथ क्या करना चाहते हैं, इसलिए मैंने उन्हें एक सरणी के रूप में रखा है।

आप नीचे दिए गए स्निपेट को चलाकर देख सकते हैं कि आपको वांछित आउटपुट मिल रहा है या नहीं।

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);
1
Som Shekhar Mukherjee 15 जिंदा 2022, 12:44
क्या आप कृपया देख सकते हैं कि क्या यह आपकी समस्या का समाधान करता है, अगर आपको कोई समस्या है तो मुझे भी बताएं। धन्यवाद
 – 
Som Shekhar Mukherjee
15 जिंदा 2022, 11:56
यह काम करता है लेकिन क्या होगा अगर मुझे प्रतिक्रिया_आईडी भी चाहिए, तो मैं अपने प्रश्न में इसका उल्लेख करना भूल गया और मैंने इसे अभी संपादित किया है
 – 
Sachin
15 जिंदा 2022, 12:32
मैंने responce_ID को भी शामिल करने के लिए अपना उत्तर अपडेट कर दिया है, कृपया एक नज़र डालें
 – 
Som Shekhar Mukherjee
15 जिंदा 2022, 12:44
1
यह काम किया धन्यवाद !!
 – 
Sachin
15 जिंदा 2022, 12:52
@ सचिन, एक दोस्ताना अनुस्मारक: यदि बटन, टेक्स्ट, शीर्षक का क्रम हमेशा सरणी में समान नहीं होता है तो एक बग होगा। उदाहरण के लिए, यदि बटन क्रम 0 में हैं और पाठ क्रम 1 में है।
 – 
ikhvjs
15 जिंदा 2022, 12:52

map, find, some और ? का उपयोग करने वाला एक उदाहरण। और ?? नलिश कोलेसिंग ऑपरेटर

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 findObjectByKey = (array, key) =>
    array.find(item => Object.keys(item).some(k => k === key))?.[key];

const output = data.responce.map(({ responce_ID, responce }) => {
    const uttence_name = Object.keys(responce)[0];
    const arr = responce[uttence_name];

    const buttons =
        findObjectByKey(arr, "buttons")
            ?.map(({ title }) => title)
            .join(" ") ?? "";

    const title = findObjectByKey(arr, "title") ?? "";

    const text = findObjectByKey(arr, "text") ?? "";

    return {
        responce_ID,
        uttence_name,
        buttons,
        title,
        text,
    };
});


console.log(output);
0
ikhvjs 15 जिंदा 2022, 12:36

आपके द्वारा दिए गए डेटा के आधार पर आवश्यक परिणाम यहां दिया गया है। कोड में, सबसे अच्छा मामला परिदृश्य है, यह जांचता है कि आपके पास टेक्स्ट युक्त ऑब्जेक्ट है या नहीं और फिर उसके अनुसार डेटा जोड़ता है। इसमें 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);
0
Alok 15 जिंदा 2022, 12:50
इस उत्तर पर एक नज़र डालें, क्योंकि यह किसी भी तरह सभी समाधानों का बेहतर संस्करण है। मुझे बताएं कि क्या यह आपकी समस्या को और अधिक दक्षता के साथ हल करता है
 – 
Alok
15 जिंदा 2022, 12:53