0

I need to use the index from a for loop to check values in 10 columns QUESTION_1_ANSWER, QUESTION_2_ANSWER, QUESTION_3_ANSWER ... etc.return from jQuery Ajax.

Return surveyResults object: {question_1_Answer: 'Other', question_1_Other_Answer: 'Naples Florida', question_2_Answer: '', question_2_Other_Answer: '', question_3_Answer: '', …}

I tried to create a variable, however it does not work. Any thoughts?

        ({  type: 'POST',
            url: 'getEmployeeSurveyResults.php', 
            dataType: 'json',
            data:   {surveyID: surveyID,
                     sessionEmployeeNumber: sessionEmployeeNumber,
                    },
            cache: false,
        
            success: function (data) { 
                                        $.each(data, function (i, surveyResults) {
                                
                                            for (i = 1; i < 11 ; i++) { 

                                                var surveyResultsQuestionAnswer='surveyResults.question_'+i+'_Answer' 

                                                console.log( 'surveyResultsQuestionAnswer: ' + surveyResultsQuestionAnswer );
                                        
                                                    if(surveyResultsQuestionAnswer !==''){ 
                                                                                       
                                                                                        Do something ....
                                                                                   }                                                                                             
                                            }                               
   
                                        });
            }, 
            error: function(jqXHR, textStatus, errorThrown) { 
                                                                alert('Error: ' + textStatus + ' ' + errorThrown) 
            }
 }); //* End - $.ajax
bbrindza
  • 1
  • 2
  • It's not entirely clear to me, but are you just looking for this?: `surveyResults['question_'+i+'_Answer']` – David Apr 18 '22 at 14:29
  • That is what I was looking for David. I would usually use this syntax to find the value in a column retuned: ***surveyResults.question_1_Answer*** Thank you . – bbrindza Apr 18 '22 at 14:33

0 Answers0