2

Please check my fiddle and tell me what I did wrong? I don't want option0. why it is coming.

var qus ={ 
    {

    "qus" :"what is your name?",
    "option0" : {"ans" : "w", "cor":"h"},
    "option1" : {"ans" : "Alex", "cor":"false"},
    "option2" : {"ans" : "Hervy", "cor":"false"},
    "option3" : {"ans" : "Rico", "cor":"true"},
    "option4" : {"ans" : "Tom", "cor":"false"},
    },

}

Here is my jsfiddle link http://jsfiddle.net/rushdi1987/jvhgxawm/4/

1
  • 1
    side-issue, but rather than manually outputting JSON, you should transform the array to the object you want, then just do JSON.stringify(obj). Much less error prone. Commented Sep 18, 2015 at 23:54

3 Answers 3

2

jsFiddle Demo

Option0 is in there because it is part of the array. The 0 index of your array is "what is your name?", in the first piece, and "what is your brother's name?" in the second.

Using for in is going to iterate each index, and taking the 0 index ends up taking that string as one of the possible answers you have. As you are assuming that [0] of the answer is the name, and [1] as the flag, the result of [0] and [1] on "what is your name?" is w and h. The index of the array is 0 at that point, so you end up with "option0" : {"ans" : w, "cor:"h"}, which you don't want.

The fix is simple, just skip that index in your for in loop using a conditional if and a continue

if(n == 0)continue;

I slightly refactored your code to make it a little easier to read as well

var objects = [
    [
        "what is your name?", 
        ["Alex", false],
        ["Hervy", false],
        ["Rico", true],
        ["Tom", false]
    ],
    [
        "what is your brother's name?", 
        ["Alex", false],
        ["Hervy", true],
        ["Rico", false],
        ["Tom", false]
    ]
];

var el = document.getElementById("out");

el.innerHTML += 'var qus ={ <br>';

for (i in objects){
    var qset = objects[i];

    el.innerHTML += '{ <br>';
    el.innerHTML += '"qus" :"' + qset[0] + '",<br>';

    for (n in qset){
        if(n == 0)continue;
        var nameset = qset[n];

        el.innerHTML += '"option' + n;
        el.innerHTML += '" : {"ans" : ' + nameset[0];
        el.innerHTML += ', "cor:"' + nameset[1] + '"},<br>';
    }

    el.innerHTML += '},<br><br>';
}
el.innerHTML += '}';
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your help. Would you mind to explain one thing more? How did that if condition fix the problem. what does that condition means ?
@rushdi - Sure. Basically what that condition is doing is skipping the 0th element in the array. Which is the first element (0 based indexing). This element is your string "what is your brother's name". So by looking to see if n == 0 we are looking to see if the current index is in fact the string "what is your brother's name". If that is the case, we do not want to process that as a set of name and boolean value we just want to skip it. The keyword continue allows us to move to the next iteration of the loop.
2

Small change:

 for (n in objects[i]) {

        if(typeof objects[i][n] !='string') {


        document.getElementById("out").innerHTML += '"option' + n + '" : {"ans" : ' + objects[i][n][0] + ', "cor:"' + objects[i][n][1] + '"},<br>';
        }
    };

If you console.log(objects[i][n]), you will notice that you have question string as element in loop. So, this way you can skip it... (or some other way, as suggested in answers)

Demo: http://jsfiddle.net/jvhgxawm/7/

However, re-structuring your initial array (if you can) - would be better option.

Comments

0

It makes more sense to store the different options as an array. So intead of:

["what is your name?", ["Alex", false],
    ["Hervy", false],
    ["Rico", true],
    ["Tom", false]
],

you should make this:

["what is your name?", [ 
    ["Alex", false],
    ["Hervy", false],
    ["Rico", true],
    ["Tom", false]
]],

then, in the inner loop, replace objects[i] with objects[i][1]:

for (n in objects[i][1]) {
    document.getElementById("out").innerHTML += '"option' + n + '" : {"ans" : ' + objects[i][1][n][0] + ', "cor:"' + objects[i][1][n][1] + '"},<br>';
};

Here you have the fiddle updated.

Hope it helps!

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.