2017-09-01 12 views
0

私はこの質問をシャッフルするのにShuffle methodを使用しました。私は私のconsole.log();でシャッフルされた値を取得していますが、私の質問のリストはシャッフルされず、私のビューで10に制限されていない1から50まで表示されます。ロダッシュを使用してシャッフルして表示に制限を表示する

ionViewDidLoad() { 
this.slides.lockSwipes(true); 
this.dataService.load().then((data) => { 
    data.map((question) => { 
    console.log(_.shuffle(data)); 
    if (data.length > 10) data.length = 10; 
    data.forEach(question => _.shuffle(question.answers)); 
    return question; 
    }); 
    this.questions = data; 
});} 

これは私のconsole.log()にあります。私question.jsonファイルで enter image description here

(私の質問の全体のスペースを取るしないように、私はほんの数を表示しています)

{ 
"questions": [ 
    { 
     "flashCardFront": "<img src='assets/images/animals.png'/>", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "These breathe, feed, grow, and leave offspring.", 
     "answers": [ 
      {"answer": "Non-living Things", "correct": false, "selected": false}, 
      {"answer": "Living Things", "correct": true, "selected": false}, 
      {"answer": "None of the above", "correct": false, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/universe.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "It is the only world that supports life.", 
     "answers": [ 
      {"answer": "Mars", "correct": false, "selected": false}, 
      {"answer": "Universe", "correct": false, "selected": false}, 
      {"answer": "Earth", "correct": true, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "It was a water world with small areas of dry land", 
     "answers": [ 
      {"answer": "Old Earth", "correct": false, "selected": false}, 
      {"answer": "Young Earth", "correct": false, "selected": false}, 
      {"answer": "Earth", "correct": true, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "According to studies of ancient rocks, life began on Earth about how many years ago?", 
     "answers": [ 
      {"answer": "5 million years", "correct": false, "selected": false}, 
      {"answer": "3300 million years", "correct": false, "selected": false}, 
      {"answer": "3800 million years", "correct": true, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/animals.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "They range from the simplest single-celled bacteria to plants, animals, and humans.", 
     "answers": [ 
      {"answer": "Living things", "correct": true, "selected": false}, 
      {"answer": "Non-living things", "correct": false, "selected": false}, 
      {"answer": "Cells", "correct": false, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "Comets and meteors rained down on what planet?", 
     "answers": [ 
      {"answer": "Earth", "correct": true, "selected": false}, 
      {"answer": "Jupiter", "correct": false, "selected": false}, 
      {"answer": "Venus", "correct": false, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "Many scientists believe that life began here.", 
     "answers": [ 
      {"answer": "Lakes and oceans", "correct": true, "selected": false}, 
      {"answer": "Planet Earth", "correct": false, "selected": false}, 
      {"answer": "Rivers and mountains", "correct": false, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "These are energy that came from hot springs on the seabed.", 
     "answers": [ 
      {"answer": "Black Matter", "correct": false, "selected": false}, 
      {"answer": "Black Smokers", "correct": true, "selected": false}, 
      {"answer": "Molecular Deposit", "correct": false, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "It is the theory of the beginning of the universe.", 
     "answers": [ 
      {"answer": "The Big Boom Theory", "correct": false, "selected": false}, 
      {"answer": "The Big Bang Theory", "correct": true, "selected": false}, 
      {"answer": "The Beginning Theory", "correct": false, "selected": false} 
     ] 
    } 
    , 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "Astronomers believe that the universe began how many years ago?", 
     "answers": [ 
      {"answer": "16 billion years", "correct": false, "selected": false}, 
      {"answer": "14 million years", "correct": false, "selected": false}, 
      {"answer": "14 billion years", "correct": true, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "These are stable particles that started to form when the universe was one second old.", 
     "answers": [ 
      {"answer": "Protons and electrons", "correct": false, "selected": false}, 
      {"answer": "Protons and neutrons", "correct": true, "selected": false}, 
      {"answer": "Neutrons and electrons", "correct": false, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "These two opposite types of particles largely destroyed each other.", 
     "answers": [ 
      {"answer": "Matter and antimatter", "correct": true, "selected": false}, 
      {"answer": "Matter and non-matter", "correct": false, "selected": false}, 
      {"answer": "Protons and matter", "correct": false, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "The universe cooled to how many degrees after the formation of hydrogen and helium nuclei.", 
     "answers": [ 
      {"answer": "30000 degrees", "correct": false, "selected": false}, 
      {"answer": "3000 degrees", "correct": true, "selected": false}, 
      {"answer": "33000 degrees", "correct": false, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "The universe was filled with clouds of what gases?", 
     "answers": [ 
      {"answer": "Helium and nitrogen gases", "correct": false, "selected": false}, 
      {"answer": "Nitrogen and hydrogen gases", "correct": false, "selected": false}, 
      {"answer": "Hydrogen and helium gases", "correct": true, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "Together with ground-based telescopes, these telescopes are now beginning to find galaxies that were created about one billion years after the Big Bang.", 
     "answers": [ 
      {"answer": "The Bubble Base Telescope", "correct": false, "selected": false}, 
      {"answer": "The Hubble Spectacle Telescope", "correct": false, "selected": false}, 
      {"answer": "The Hubble Space Telescope", "correct": true, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "This begins about 100 km above the Earth, where the shell of air around our planet disappears.", 
     "answers": [ 
      {"answer": "Atmosphere", "correct": false, "selected": false}, 
      {"answer": "Outer Space", "correct": true, "selected": false}, 
      {"answer": "Ionoshpere", "correct": false, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "The visible universe is at least how many light years across?", 
     "answers": [ 
      {"answer": "95 billion light years", "correct": false, "selected": false}, 
      {"answer": "89 million light years", "correct": false, "selected": false}, 
      {"answer": "93 billion light years", "correct": true, "selected": false} 
     ] 
    }, 
    { 
     "flashCardFront": "<img src='assets/images/earth.png' />", 
     "flashCardBack": "", 
     "flashCardFlipped": false, 
     "questionText": "These are considered the strongest objects in the Universe.", 
     "answers": [ 
      {"answer": "Supernovae", "correct": false, "selected": false}, 
      {"answer": "Black holes", "correct": true, "selected": false}, 
      {"answer": "Gravitational fields", "correct": false, "selected": false} 
     ] 
    } 
] 

}

答えて

0

Lodashの_shuffleメソッドが非であります変異体。シャッフルされた配列を表示したい場合は、それを表示した場所でシャッフルするか、古い配列のシャッフルされた新しいバージョンとして配列を複製してください。ここでは何が起こるか

注:

var x = [1, 2, 3, 4] 
undefined 
_.shuffle(x) 
(4) [4, 3, 2, 1] 
x 
(4) [1, 2, 3, 4] 
+0

私は本当に理解することはできません。それを変数に保存することを意味しますか? – AngularNewbie

+0

はい、次のようなものでコピーを作ることができます: 'var mutatedArray = ._shuffle(originalArray)' 'mutatedArray'は元の配列の変異(シャッフル)されたバージョンを保持するはずです。 – jmargolisvt

+0

私はそれを保存しますが、私はそれを表示できません。 – AngularNewbie

関連する問題