0

I've been trying to map an array in a random way. I want every object of the array to show up. But the order should always be different and random on refresh.

const [ list ] = [
        {id: 0, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 1, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 2, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 3, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 4, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 5, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 6, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 7, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 8, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 9, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false}
    ];

 {list.map((item) => (...


3
  • 1
    How do you define random? Did you make an attempt already that did not give you desired results? Commented Nov 17, 2020 at 20:14
  • BTW... const [ list ] = [ x, y, z] ends in list beeing x; I think you want const list = [ x, y, z]; Commented Nov 17, 2020 at 21:03
  • See stackoverflow.com/questions/2450954/…, to choose an efficient and correct implementation of the shuffle operation Commented Nov 17, 2020 at 21:27

3 Answers 3

7

I think it would be more useful to shuffle the array. And you should also change the definition of the constant.

You could try something like this:

const shuffle = arr => [...arr].sort(() => Math.random() - 0.5);

const list = [
  {id: 0, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},
  {id: 1, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},
  {id: 2, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},
  {id: 3, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},
  {id: 4, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},
  {id: 5, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},
  {id: 6, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},
  {id: 7, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},
  {id: 8, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false},
  {id: 9, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false}
];

const newList = shuffle(list);

console.log(newList);

Sign up to request clarification or add additional context in comments.

3 Comments

Shuffling the array is a bad idea in react because react uses immutable data paradigm.
Nice, so +1 from me
I didn't want to turn this into such a big discussion! :D But thank you for your solution. This way it works out. The last problem ist, that on every state change it shuffles again
1

Here is a pure function with immutable data paradigm:

const shuffleArray = (arr) => {
    // leave arr as it is (immutable data in react)
    const copy = [...arr];
    // our output array
    const output = [];
    // while there are items
    while (copy.length > 0) {
        // removes 1 random element from copy and adds it to output;
        output.push(copy.splice(Math.floor(Math.random() * copy.length), 1));
    }
    // return our random array
    return output;
};

Comments

-1

You should build a function to shuffle items randomly. Try below.

const list = [
        {id: 0, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 1, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 2, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 3, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 4, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 5, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 6, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 7, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 8, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false},
        {id: 9, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false}
    ];
    
const shuffle = array => {
  let currentIndex = array.length,
    temporaryValue,
    randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {
    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
};

console.log(shuffle(list))

2 Comments

react uses immutable data paradigm. Shuffling the array is a bad idea.
He asked explicitly for a mapping function. your funtion isnt mapping, its just manipulation the original data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.