Shuffling Multiple arrays Javascript [duplicate]

≡放荡痞女 提交于 2019-12-02 13:59:11

You should use map and sort methods.

First of all, you can generate a random number for every item in the array, using Math.random method. Next step is to sort array by this generated number.

Last step is to create the sentence with the items of the array, using join method.

var array1 = ["The", "Man", "and", "his", "dog"];
console.log(array1.map(function(n){ 
              return [Math.random(), n];
            }).sort().map(function(item){
                return item[1] ;
           }).join(' '));

This should do it for you....

["The", "Man", "and", "his", "dog"]
      .map(w => { return { seq: Math.random(), word: w } })
        .sort((a,b) => a.seq<b.seq?-1:1)
          .map(w => w.word)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!