简体   繁体   English

随机排列的对象无法正常工作

[英]Shuffle array of objects doesn't work as expected

Shuffle array of objects doesn't work as expected 随机排列的对象无法正常工作

I've tried all different kinds of shuffle methods i have found here. 我尝试了在这里找到的所有各种随机播放方法。 If I run them on js.fiddle everything works fine, but since I use it in my code it doesn't shuffle anymore. 如果我在js.fiddle上运行它们,则一切正常,但是由于我在代码中使用了它,因此不再混乱。 There is no error message or anything. 没有错误信息或其他任何信息。 It just doesn't do anything. 它什么也没做。 I've read all threads here about shuffling objects but I've found nothing which fixes this problem. 我已经阅读了有关改组对象的所有线程,但没有发现任何可解决此问题的方法。

I use an API from https://randomuser.me/ to get random users. 我使用https://randomuser.me/中的API来获取随机用户。 Those are stored in an array of objects which I want to shuffle and then render to the UI. 那些存储在我要洗牌然后呈现到UI的对象数组中。 I have a generator class to fetch the data and I store everything in a state object. 我有一个生成器类来获取数据,并将所有内容存储在状态对象中。 I'm wondering if it's maybe something about the async function because all of that is pretty new to me. 我想知道这是否与异步功能有关,因为所有这些对我来说都是很新的。

export const shuffle = (array) => {
    let currentIndex = array.length;
    let temporaryValue;
    let randomIndex;
    const newArray = array.slice();
    // While there remains elements to shuffle...
    while (currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        // Swap it with the current element.
        temporaryValue = newArray[currentIndex];
        newArray[currentIndex] = newArray[randomIndex];
        newArray[randomIndex] = temporaryValue;
    }
    return newArray;
};

And here is the other code where I also call the shuffle function 这是我也称为shuffle函数的其他代码

/* Global state of the app

 */

const state = {};

const startGame = async() => {

    // 1) New Generator and add to state
    state.generator = new Generator();

    // 2) Prepare UI for Rendering
    renderer.clearContent();

    // 3)  Call API for new User
    await state.generator.generateUser();

    // 4) Render user to UI
    renderer.renderResults(state.generator.user);

    // 5) Start a timer
    $("#CountDownTimer").TimeCircles().start();


    // When Timer hits 0 -------------->

    $("#CountDownTimer").TimeCircles().addListener(function(unit, value, total) {
        if (total < 0) {
            //1) Clear HTML Content and Stop Timer
            elements.playGround.innerHTML = "";
            $("#CountDownTimer").TimeCircles().stop();
            //2) shuffle Person Object
            shuffle.shuffle(state.generator.user);
            console.log(state.generator.user)

            //3) Display Image  
        }
    });
}

timer.displayTimer();

elements.startButton.addEventListener('click', element => {
    element.preventDefault();
    startGame();
})

Notice your shuffle function return the new array but when you call it at shuffle.shuffle(state.generator.user); 注意您的shuffle函数返回数组,但是当您在shuffle.shuffle(state.generator.user);处调用它时shuffle.shuffle(state.generator.user); you don't assign the return value to nothing... 您不会将返回值分配为零...

Try something like: 尝试类似:

state.generator.user = shuffle.shuffle(state.generator.user);

Your shuffle method is immutable and your not using the return value of it when you call it. 您的shuffle方法是不可变的,调用时不使用它的返回值。

Remove this line: const newArray = array.slice(); 删除此行: const newArray = array.slice(); and replace newArray with array 并将newArray替换为array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM