-1
{
    surveyCode:123456,
    q1:{
        question:'What is your name?',
        option1:{
            type:'text',
            placeholder:'Enter your name',
            header:''
        }
    },
    q2:{
        question:'What grade are you in?',
        option1:{
            type:'radio',
            content:'9th'
        },
        option2:{
            type:'radio',
            content:'10th'
        },
        option3:{
            type:'radio',
            content:'11th'
        },
        option4:{
            type:'radio',
            content:'12th'
        }
    }
}

Every time I try to iterate through this in my ejs file all I get is [object][Object], how can I iterate though this to get all the objects inside these objects out and stored in variables. The object is being retrieved from my mongo database and being passed to my ejs view.

Lewis A
  • 1
  • 1

1 Answers1

0

You can use pure JS

var obj = {
    surveyCode:123456,
    q1:{
        question:'What is your name?',
        option1:{
            type:'text',
            placeholder:'Enter your name',
            header:''
        }
    },
    q2:{
        question:'What grade are you in?',
        option1:{
            type:'radio',
            content:'9th'
        },
        option2:{
            type:'radio',
            content:'10th'
        },
        option3:{
            type:'radio',
            content:'11th'
        },
        option4:{
            type:'radio',
            content:'12th'
        }
    }
};
console.log(Object.keys(obj)); // console: ["surveyCode", "q1", "q2"]

From your example, one can do:

Object.keys(obj).forEach(function(key){
  <%- obj[key] %> //key will be q# the output will be Question details 
})
0bserver07
  • 3,390
  • 1
  • 28
  • 56