lundi 25 décembre 2017

How to handle Null value from API Response in Vue Js

I am trying to show value getting from API. I am trying to show question_section name in template. I am getting these value multiple time means an Array object in json. So One Time i am getting value question_section like " A: ABC Xyz " but next 5 time null value again " B: DEF pqr" and 5 time null value.

So when i an trying to show this in my template then getting undefined or null. I used JSON.stringfy(value1.question_section) then null because all value is null. here I noticed that it is printing value of last index (4th index) but it should print of Zero index.

my Code of Vue js

    $.each(this.questionPaperAll, function( key, value) {

        $.each(value.questions, function( key1, value1) {

           if( JSON.stringify( value1.question_section) )
          {
             title = "<div style='font-weight: bold;padding-bottom: 10px'>"

              + JSON.stringify( value1.question_section)  +" &nbsp;" + 

                  value1.section_marks + "</div>\n" +

                 "<div style='font-weight: bold;padding-bottom: 10px'>" + 

                  s_no + ") " + value1.question_type_name[0].name + "

                  </div>\n" 
                   }     
                        });
              });

My API Json Response :: i can not show here full response it is just sample where i am facing problem..

     [ {
            "question_type_name": "4",
            "questions": [
                {
                    "question_ref_id": "DndoURx54fj7HaSF",
                    "question_section": "A : Attempt All question.",
                    "section_marks": "10", 
                    "question_name": "economics Question 122",
                },
                {
                    "question_ref_id": "8wFqcPBVruJSvYKv",
                    "question_section": "",
                    "section_marks": "10",
                    "question_name": "economics Question 127",

                },
                {
                    "question_ref_id": "VU4xhxMYO5pdGkNM",
                    "question_section": "",
                    "section_marks": "10",   
                    "question_name": "economics Question 128",
                },
               {
                    "question_ref_id": "VU4xhxMYO5pdGkNM",
                    "question_section": "",
                    "section_marks": "10",   
                    "question_name": "economics Question 128",
                },
                 {
                    "question_ref_id": "VU4xhxMYO5pdGkNM",
                    "question_section": "",
                    "section_marks": "10",   
                    "question_name": "economics Question 128",
                }
            ]
        },
        {
            "question_type_name": "1",
            "questions": [
                {
                    "question_ref_id": "e0NGIyFmsQ5EEKiX",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 333",   
                },
                {
                    "question_ref_id": "e0NGIyFmsQ5EEKiX",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 333",   
                },
                {
                    "question_ref_id": "e0NGIyFmsQ5EEKiX",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 333",   
                },
             {
                    "question_ref_id": "e0NGIyFmsQ5EEKiX",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 333",   
                },
               {
                    "question_ref_id": "e0NGIyFmsQ5EEKiX",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 333",   
                },
            ]
        },
        {
            "question_type_name": "3",
            "questions": [
                {
                    "question_ref_id": "oALoJ2nCzQm2EBbA",
                    "question_section": "B : Attempt Only 5 Question from each Question.",
                    "section_marks": "20",
                    "question_name": "economics Question 11w",
                    "marks": "1",
                },
                {
                    "question_ref_id": "YUrzwnrW2o0nXMD8",
                    "question_section": "",
                    "section_marks": "20",
                    "question_name": "economics Question 12l",
                    "marks": "1",
                },
                   {
                    "question_ref_id": "YUrzwnrW2o0nXMD8",
                    "question_section": "",
                    "section_marks": "20",
                    "question_name": "economics Question 12l",
                    "marks": "1",
                },
                {
                    "question_ref_id": "YUrzwnrW2o0nXMD8",
                    "question_section": "",
                    "section_marks": "20",
                    "question_name": "economics Question 12l",
                    "marks": "1",
                },
               {
                    "question_ref_id": "YUrzwnrW2o0nXMD8",
                    "question_section": "",
                    "section_marks": "20",
                    "question_name": "economics Question 12l",
                    "marks": "1",
                },
                  {
                    "question_ref_id": "YUrzwnrW2o0nXMD8",
                    "question_section": "",
                    "section_marks": "20",
                    "question_name": "economics Question 12l",
                    "marks": "1",
                }
            ]
        },
        {
            "question_type_name": "2",
            "questions": [
                {
                    "question_ref_id": "KztHaRUnRCLZwx8U",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 5",
                    "marks": "1",
                },
                {
                    "question_ref_id": "KztHaRUnRCLZwx8U",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 5",
                    "marks": "1",
                },
                {
                    "question_ref_id": "KztHaRUnRCLZwx8U",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 5",
                    "marks": "1",
                },
                {
                    "question_ref_id": "KztHaRUnRCLZwx8U",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 5",
                    "marks": "1",
                },
                {
                    "question_ref_id": "KztHaRUnRCLZwx8U",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 5",
                    "marks": "1",
                },
                                  {
                    "question_ref_id": "KztHaRUnRCLZwx8U",
                    "question_section": "",
                    "section_marks": null,
                    "question_name": "economics Question 5",
                    "marks": "1",
                },
            ]
        }]

This is sample response for understand problem. you can see that array of value. where Array questions have key question_section that value first time some string but rest all null until new section not come. so i want to show only those question_section whose value have and rest avoid mean not show. for this i used if condition but it now working. why I don't know? so please correct may code. also explain what is going wrong.

also i want to tell that when i try to check which one index of questions array is printing. i saw index 4 that was last index of first array. why it is printing last.

questionPaperAll is array that holding API response when i am getting from server.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire