简体   繁体   English

javascript JSON多维数组

[英]javascript multidimensional array to JSON

Please check my fiddle and tell me what I did wrong? 请检查我的小提琴,并告诉我我做错了什么? I don't want option0. 我不要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/ 这是我的jsfiddle链接http://jsfiddle.net/rushdi1987/jvhgxawm/4/

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. 如果您使用console.log(objects [i] [n]),则会发现问题字符串作为循环中的元素。 So, this way you can skip it... (or some other way, as suggested in answers) 因此,您可以通过这种方式跳过它...(或其他方式,如答案中所建议)

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

However, re-structuring your initial array (if you can) - would be better option. 但是,重新构造您的初始数组(如果可以)是更好的选择。

jsFiddle Demo

Option0 is in there because it is part of the array. Option0在其中,因为它是数组的一部分。 The 0 index of your array is "what is your name?" 数组的0索引是"what is your name?" , in the first piece, and "what is your brother's name?" 在第一篇中,以及"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. 使用for in将会迭代每个索引,而采用0索引最终会使该字符串作为您可能的答案之一。 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?" 假设答案的[0]是名称,并且[1]作为标志,则[0][1]在“您叫什么名字?”上的结果 is w and h . wh 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. 此时数组的索引为0,因此您最终"option0" : {"ans" : w, "cor:"h"},使用"option0" : {"ans" : w, "cor:"h"},

The fix is simple, just skip that index in your for in loop using a conditional if and a continue 修复很简单,只需使用条件if和continue跳过for in循环中的索引

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 += '}';

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] : 然后在内部循环中,将objects[i]替换为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! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM