【问题标题】:Using lodash to shuffle and limiting displaying on view使用 lodash 随机播​​放和限制视图显示
【发布时间】:2018-02-10 20:07:54
【问题描述】:

我用这个Shuffle method 来打乱我的问题。我在我的 console.log(); 上得到了洗牌的值,但它仍然显示我的问题列表,从 1 到 50 没有洗牌,而且在我看来不限于 10。

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 文件上(我只显示了几个,以免占用我的问题的整个空间)

{
"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}
        ]
    }
]

}

【问题讨论】:

    标签: javascript arrays json typescript ionic-framework


    【解决方案1】:

    Lodash 的 _shuffle 方法是非可变的。如果你想展示洗牌后的数组,要么在你展示它的地方洗牌,要么将你的数组克隆为旧数组的一个新的、洗牌的版本。

    注意这里发生了什么:

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

    【讨论】:

    • 我真的无法理解。您的意思是将其保存在变量中吗?
    • 是的,您可以使用以下内容制作副本:var mutatedArray = ._shuffle(originalArray) mutatedArray 现在应该包含原始数组的变异(改组)版本。
    • 我保存了但无法显示。
    • 太棒了!如果对您有帮助,请不要忘记接受我的回答。 ;)
    猜你喜欢
    • 2014-03-25
    • 1970-01-01
    • 2017-04-12
    • 2019-04-14
    • 1970-01-01
    • 2023-01-23
    • 2023-03-23
    • 2015-11-16
    • 1970-01-01
    相关资源
    最近更新 更多