javascript - jquery create nested object by adding properties in each - Error Uncaught TypeError: .....of undefined -


i got code

var dash = {};  function saveresults() {     var sectname = 'answers';      if(!dash.hasownproperty(sectname)) {       dash[sectname] = {}     }      $('.selected').each(function () {         sectname = $(this).closest('.question-wrapper').data('group');          var question = $(this).closest('.question-wrapper').find('.question span').text().trim().replace(/\s{2,}/g, ' ');         dash[sectname][question] = $(this).val();     }); } 

i want create nested object section name , question name answers inside. code gives

main.js:373 uncaught typeerror: cannot set property 'questiontext' of undefined 

not sure structire want answers , questions have. if block should in loop block work properly.

var dash = {};  function saveresults() {   var sectname = 'answers';    $('.selected').each(function () {     sectname = $(this).closest('.question-wrapper').data('group');      if(!dash.hasownproperty(sectname)) {       dash[sectname] = {}         var question = $(this)                      .closest('.question-wrapper')                      .find('.question span')                      .text().trim().replace(/\s{2,}/g, ' ');       dash[sectname][question] = $(this).val();   }  }); } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -