“javascript remanie un tableau” Réponses codées

comment trier au hasard un tableau javascript

array.sort(() => Math.random() - 0.5);
Real Rocket Raccoon

Array JS Shuffle

yourArray.sort(function() { return 0.5 - Math.random() });
Slocaly

javascript remanie un tableau

const shuffleArray = (arr) => arr.sort(() => 0.5 - Math.random());

console.log(shuffleArray([1, 2, 3, 4]));
// Result: [ 1, 4, 3, 2 ]
Rashid Siddique

Comment mélanger un tableau javascript

/**
 * Shuffles array in place.
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = round(random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}

shuffle(array);
Timmber

mélanger un tableau de nombres en javascript

let numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
numbers = numbers.sort(function(){ return Math.random() - 0.5});
/* the array numbers will be equal for example to [120, 5, 228, -215, 400, 458, -85411, 122205]  */
Creepy Gábor

Arraie de mélanger javascript

/**
 * Shuffles array in place.
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}


/**
 * Shuffles array in place. ES6 version
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    for (let i = a.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [a[i], a[j]] = [a[j], a[i]];
    }
    return a;
}

var myArray = ['1','2','3','4','5','6','7','8','9'];
shuffle(myArray);
Blue Bear

Réponses similaires à “javascript remanie un tableau”

Questions similaires à “javascript remanie un tableau”

Plus de réponses similaires à “javascript remanie un tableau” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code