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
Post a Comment