首页 > 解决方案 > 如何使用地图洗牌数组

问题描述

我尝试过使用 map.shuffle 字符串单词数组。

该函数应该接受一个单词数组,例如: ["Apple","Coffe","Bananas"].

之后,它应该返回一个新的随机数组。一些随机的例如: ["Coffe",Apple,"Bananas].

但我很少明白如何做到这一点。

function shuffleArray(names) {
  if (typeof names != "object")
    throw "not an array"
  var mapResult = names.map(function(item, index, array) {
    return index + Math.floor(Math.random() * (index + 1));
  });
  return mapResult;
}

console.log(shuffleArray(["Apple","Coffe","Bananas"]))

标签: javascriptarraysshuffle

解决方案


推荐阅读