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/
3 Answers 3
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 += '}';
2 Comments
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.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
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!
JSON.stringify(obj). Much less error prone.