質問

I'm learning JS and have to do a trivia game, where each question has 4 possible answers. The questions are given to me in a string with "#", which separates the questions, and "@", which separates each question in its id, its text, and its correct answer. For the answers I have a string with similar format: idQuestion@idAnswer@textOfAnswer# I know I did it in a very precarious way (I'm using 4 arrays! ._.), but I can't figure another way to do it. This is the code where I start from the string which contains the answers, and transform each answer in an "associative array", which is saved in an indexed answer... called assocAnswers (wtf xD):

var indexAnswers = strAnswers.split("#");
var auxArray = new Array ();
var auxArray2 = new Array();
var assocAnswers = new Array ();
for (var b=0; b<indexAnswers.length; b++){
    auxArray = indexAnswers[b].split("@");
    auxArray2 = {
        "idQuestion":auxArray[0],
        "idAnswer":auxArray[1],
        "txtAnswer":auxArray[2]
    }
    assocAnswers.push(auxArray2);
}

I want to know how can I manage to check if the value of idQuestion (which is the first key in each "associative array" that occupies one element of the indexed array) equals x number. Once I checked that, I have to find the remaining 3 answers with also match the idQuestion, and display the four answers in a div (so I have to display the value of the key txtAnswer), each answer in a <label> inside a radio input, but I think I don't need help with the jQuery part. BTW, I haven't learned the for...in loop yet, so if anyone uses it for a possible solution, any explanation of that will be more than welcomed :P Thanks!

Edit: Some examples of the strQuestions in english (strAnswers remains in spanish):

var strQuestions = "1@How is a function without parameters declared?@2#"
+ "2@Which of the next invocations is right?@4#"
+ "3@Given a function: myFunction(a,b) Which is the correct invocation?@3#";

var strAnswers =
"1@1@ function:myFunction()#1@2@ function myFunction()#1@3@ declare myFunction()#1@4@ new myFunction#2@1@Resultado=myFunction#2@2@Call myFunction()#2@3@myFunction#2@4@Resultado=myFunction()#3@1@MiFuncion a,b#3@2@a=MiFuncion(b)#3@3@MiFuncion(x,y)#3@4@Ninguna de las anteriores#
役に立ちましたか?

解決 2

I got pretty lazy while writing it, but this fiddler will show how you can parse it and this turns it into an array of objects so you can work with it easier. There is also a method to easily find an element based on a collection and the id of the element.

There probably is a better way to for the for loops but I can't think of it off the top of my head at the moment. I will update it if I can remember.

The JSfiddler.

Note: The anwserId is stored in the .key property

他のヒント

Let me see if I understand your problem correctly...

I know I did it in a very precarious way (I'm using 4 arrays! ._.), but I can't figure another way to do it.

var data = '1@1@hello#2@1@foo#1@2@world';

var questions = data.split('#').map(function(q) {
  q = q.split('@');
  return {
    idQuestion: q[0],
    idAnswer: q[1],
    text: q[2]
  };
});

Now you have your object with key:value pairs. This is how you can filter by any criteria:

var filteredQuestions = questions.filter(function(q) {
  return q.idQuestion == 1;
});

console.log(filteredQuestions); //=> [{...text: "hello"}, {...text: "world"}]

Is this what you're looking for?

var indexAnswers = strAnswers.split("#");
var auxArray = new Array ();
var auxArray2 = new Array();
var assocAnswers = new Array ();
for (var b=0; b<indexAnswers.length; b++){
    auxArray = indexAnswers[b].split("@");
    if (auxArray[0].toString() == someId.toString()) {
        ... Fill your page
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top