javascript - Two dimentional array in js -
i trying 2 dimensional array in javascript, have json , trying populate 2d array. code here:
jquery.each(data.data, function(key,val){ survey_sec_id_arr.push(val.section_id); survey_question_id_arr.push(val.section_id); jquery.each(val, function(key1,val1){ if(key1!="section_id" && key1!="section_name"){ survey_question_id_arr[val.section_id].push(val1.question_id); } }); }); console.log(survey_question_id_arr);
so here error in firebug is:
typeerror: survey_question_id_arr[val.section_id] undefined
what going wrong?
you loop not correct. add of elements via push array looks
survey_question_id_arr = array( [0] => val.section_id, [1] => val.section_id...)
then try element of array via
//returns undefined survey_question_id_arr[val.section_id] //should return value val.section_id[0]
fix:
survey_question_id_arr[val.section_id].push(val1.question_id)
i think solves problem
Comments
Post a Comment