2016-04-13 5 views
0

Wie kann ich meine folgende JSON in angularjs randomisieren ???Wie man JSON in Angularjs randomisiert

{ 
    "1": { 
      "id": "1", 
      "que": "Grand Central Terminal, Park Avenue, New York is the world's", 
      "options": { 
       "A": "largest railway station", 
       "B": "highest railway station", 
       "C": "longest railway station", 
       "D": "None of the above" 
      }, 
      "ans": "A" 
     }, 
"2": { 
      "id": "2", 
      "que": "Entomology is the science that studies", 
      "options": { 
       "A": "Behavior of human beings", 
       "B": "Insects", 
       "C": "The origin and history of technical and scientific terms", 
       "D": "The formation of rocks" 
      }, 
      "ans": "B" 
     }, 
"3": { 
      "id": "3", 
      "que": "Eritrea, which became the 182nd member of the UN in 1993, is in the continent of", 
      "options": { 
       "A": "Asia", 
       "B": "Africa", 
       "C": "Europe", 
       "D": "Australia" 
      }, 
      "ans": "B" 
     } 
} 
+0

Was meinen Sie mit Randomisierung eines JSON? –

+0

Ich arbeite an Quiz-Anwendung für das ich meine Fragen randomize ... Wie kann ich diese Fragen randomize? Ich habe bereits meine JSON konvertiert mit $ map – Rudrika

Antwort

0

Um die Aufgabe zu erleichtern würde ich vorschlagen, dass Sie verwenden ein Array von Objekten wie diesem:

var questions= 
[ 
    { 
     "id": "1", 
     "que": "Grand Central Terminal, Park Avenue, New York is the world's", 
     "options": { 
      "A": "largest railway station", 
      "B": "highest railway station", 
      "C": "longest railway station", 
      "D": "None of the above" 
     }, 
     "ans": "A" 
    }, 
    { 
     "id": "2", 
     "que": "Entomology is the science that studies", 
     "options": { 
      "A": "Behavior of human beings", 
      "B": "Insects", 
      "C": "The origin and history of technical and scientific terms", 
      "D": "The formation of rocks" 
     }, 
     "ans": "B" 
    }, 
    { 
     "id": "3", 
     "que": "Eritrea, which became the 182nd member of the UN in 1993, is in the continent of", 
     "options": { 
      "A": "Asia", 
      "B": "Africa", 
      "C": "Europe", 
      "D": "Australia" 
     }, 
     "ans": "B" 
    } 
] 

Und dann t o Wählen Sie eine zufällige Frage aus:

$scope.randomQuestion = questions[Math.floor(Math.random()*questions.length)]; 
+0

danke alles .. Ich werde versuchen, Sie zu aktualisieren, wenn irgendwelche Probleme da sind – Rudrika

0

Eine Lösung wäre, um Ihre Fragen zu einem Array-Objekt zu konvertieren, so können Sie die Fragen randomisieren bestellen:

http://plnkr.co/edit/PSeh0BLNjK5ydXJrLguw?p=preview

<p ng-repeat="q in questionsArray | orderBy: random">{{q.key}} - {{q.val.que}}</p>