0

Beginner javascript programmer here, and I'm trying to create a complicated variable.

var mainList = [{
    value : 1, 
    mainQuestion : 'Question1', 
    answerChoices : ['Answer 1', 'Answer 2', 'Answer 3']
},
{
    value : 2,
    mainQuestion : 'Question 2', 
    answerChoices : ['Answer 1', 'Answer 2']
}];

The issue I'm having, is that I need to associate values with the answers. This is going to be a set of questions, that narrows things down to where it helps get you to the right information to help solve your problem.

So how can I associate a value with the answers (Answer 1, Answer 2) ? Basically I'm needing the value associated with these answers to be added to a list element on the page, so that I can continue with further questions. I've tried turning them into another object, but keep getting console errors, and can't get it to work.

Any help is greatly appreciated.

Edit: Wow, you guys are fast and super helpful. Consider this resolved, as I've been given plenty of information to get this to work. Thank you so much!

0

4 Answers 4

1

This is not a JS question per se, but a general data structure question. You can have a separate array of values, or you can have objects in the answer choices array:

    var mainList = [{
      value : 1, 
      mainQuestion : 'Question1', 
      answerChoices : ['Answer 1', 'Answer 2', 'Answer 3'],
      answerValues: [10,20,15]
    },{
      value : 2,
      mainQuestion : 'Question 2', 
      answerChoices : ['Answer 1', 'Answer 2']},
      answerValues: [6,8]
   ];

Or as an object:

    var mainList = [{
      value : 1, 
      mainQuestion : 'Question1', 
      answerChoices : [{name:'Answer 1',value:10,{name: 'Answer 2',value:20},{name: 'Answer 3',value:15}]
    },{
      value : 2,
      mainQuestion : 'Question 2', 
      answerChoices : [{name:'Answer 1',value:6},{name: 'Answer 2',value:8}]}
   ];

If you have console errors with that, post them as a separate q.

Sign up to request clarification or add additional context in comments.

Comments

0

Make answerChoices an object, not an array, whose keys are the answers and values are the values you want to associate:

answerChoices: {
    'Answer 1': value1,
    'Answer 2': value2,
    'Answer 3': value3
}

Or it could be an array of object:

answerChoices: [
    { answer: 'Answer 1', value: value1 },
    { answer: 'Answer 2', value: value2 },
    { answer: 'Answer 3', value: value3 }
]

The first is appropriate if you only need to be able to look up values based on the answer. The second is appropriate if the order of answers must be maintained; to find the value based on an answer, you'll have to search the choices with a loop.

Comments

0

You could also key your object by the value (essentially the ID, of the question), something along the lines of:

{
  "1": {"value": 1, "mainQuestion": "Question1", "answerChoices": ['1', '2', '3']},
  "2": {"value": 2, "mainQuestion": "Question2", "answerChoices": ['4', '5', '6']}
}

Comments

0

Here's a full HTML page that does what you're asking. It displays the appropriate answers in a list on the page.

<html>

<head>
    <script>
        //Your object
        var mainList = [{
            value: 1,
            mainQuestion: 'Question1',
            answerChoices: ['Answer 1', 'Answer 2', 'Answer 3']
        }, {
            value: 2,
            mainQuestion: 'Question 2',
            answerChoices: ['Answer 1', 'Answer 2']
        }];
        //Takes in the value you want to get the answers for, 
        //and appends them in a list to the ul element on the page
        function loadAnswers(value) {
                for (i in mainList) {
                    if (mainList[i].value == value) {
                        for (j in mainList[i].answerChoices) {
                            document.getElementById("myList").innerHTML += "<li>" + mainList[i].answerChoices[j] + "</li>"
                        }
                    }
                }
            }
            //When the page loads, execute the function
        window.onload = function() {
            //Pass the value which you want to display
            //the answers for to loadAnswers.
            loadAnswers(1)
        };
    </script>
</head>

<body>
    <ul id="myList"> </ul>
</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.