我对JS完全陌生,这是我得到的json。
我需要按答案顺序找出所有答案,并获取正确答案的索引。

有参考吗?

 //what i got
    var questions =
    {"Q_id":1, "Q_question":"why yellow minions?",
    "Q_correctFB":"Very good!",
    "Q_notCorrectFB":"try again...",
    "Q_skill":"disney",
    "Q_answers_lst": [
        {"A_id":1, "Q_id":1, "answer":"too much bananas!","isCorrect":1},
        {"A_id":2, "Q_id":1, "answer":"all minions are cowards","isCorrect":0},
        {"A_id":3, "Q_id":1, "answer":"liver problems","isCorrect":0}
    ]
    };


我只需要列出所有可能的答案,就是这样:

//what i need

var ans_lst = ["too much bananas!", "all minions are cowards", "liver problems"]


我知道我应该开始:

var lst = questions.Q_answers_lst;


但是然后如何清除标记的数据?
谢谢

最佳答案

使用可以使用reduce对此进行存档



var questions =
    {"Q_id":1, "Q_question":"why yellow minions?",
    "Q_correctFB":"Very good!",
    "Q_notCorrectFB":"try again...",
    "Q_skill":"disney",
    "Q_answers_lst": [
        {"A_id":1, "Q_id":1, "answer":"to much bananas!","isCorrect":1},
        {"A_id":2, "Q_id":1, "answer":"all minions are cowards","isCorrect":0},
        {"A_id":3, "Q_id":1, "answer":"liver problems","isCorrect":0}
    ]
    };

var answers = questions.Q_answers_lst.reduce(function (acc, value) {
  return acc.concat(value.answer);
}, []);
console.log(answers);

关于javascript - 简单的json解析以创建新列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42894440/

10-12 06:32