js数组内元素随机打乱

294 阅读1分钟
    /* 我们总会需要得到随机的数据,但是我们需要从特定的数据种来获取
        那么你可以使用下面的小技巧
    */
    
    let array = [11, 12, 13, 14, 150, 15, 555, 556, 545];
    let randomArray = array.sort(()=>Math.random()-0.5)
    console.log(randomArray); // 13, 15, 12, 556, 11, 14, 555, 545, 150]
    
    // 当然,生成的随机值,每次运行代码都会有所不同,上面的console.log 我只取了其中一种