如何在javascript中定义多个数组

时间:2016-08-02 00:36:58

标签: javascript arrays

所以我在这里有一些代码: 首先是一个简单的随机#生成器和一个数组选择函数:

function Rand(min, max) {
  return parseFloat(Math.floor(Math.random() * max - min + 1))) + parseFloat(min);
}

function Choose(arr) {
  //Returns an element from an array at random.
  return arr[Math.floor(Math.random() * arr.length)];
}

和第二张牌洗牌:

function CardDeck() {
  var Cd = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"];
  var H = [];
  var S = [];
  var D = [];
  var C = [];
  var Result = [];
  var Dk = document.getElementById("Deck Count").value;

  for (i = 0; i < Cd.length; i++) {
    S[i] = Cd[i] + " of Spades";
    H[i] = Cd[i] + " of Hearts";
    C[i] = Cd[i] + " of Clubs";
    D[i] = Cd[i] + " of Diamonds";
  }

  if (Dk == "4") {
    P = S.concat(C, D, H);
  } else if (Dk == "5") {
    var St = [];
    for (i = 0; i < Cd.length; i++) {
      St[i] = Cd[i] + " of Stars";
    }
    P = S.concat(C, D, H, St);
  } else if (Dk == "6") {
    var Rk = [];
    var Wh = [];
    for (i = 0; i < Cd.length; i++) {
      Rk[i] = Cd[i] + " of Rackets";
      Wh[i] = Cd[i] + " of Wheels";
    }
    P = S.concat(C, D, H, Rk, Wh);
  }

  for (i = 0; i < Dk * Cd.length; i++) {
    var Q = Choose(P);
    R = P.indexOf(Q);
    Result[i] = (i + 1) + ": " + Q;
    P = P.slice(0, R).concat(P.slice(R + 1));
  }

  document.getElementById("Cards").innerHTML = Result.join("\n");
}

有没有一种简单的方法可以让它更快或者至少更快地声明数组,而不是仅为每个执行a = [],b = [] ....因为这可能既费时又乏味。

此外,有没有办法在基础JS中获得更长时间的更好的RNG(没有库,因为它是基于JS构建的)

4 个答案:

答案 0 :(得分:1)

你可以避免像这样反复输入varvar H=[], S=[], D=[], C=[];但是否则很难比你拥有的更简洁。

答案 1 :(得分:0)

你可以这样做:声明一个数组根,然后在for循环中声明很多子数组:

var arrays = [];
var nmbOfAr = 20;
for(int i = 0; i < nmbOfAr; i++){
    arrays[i] = [];
}

答案 2 :(得分:0)

如果将套装名称存储在另一个数组中,则会消除很多变量;然后将applyconcat一起使用可以一次性将它们全部转换为Result

function CardDeck() {
  var Cd = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"];
  var Result = [];
  var Dk = document.getElementById("Deck Count").value;

  // Put the suit names into a new array
  var suitNames = [" of Spades", " of Hearts", " of Clubs", " of Diamonds"];
  if (Dk == "5") {
    suitNames.push(" of Stars");
  } else if (Dk == "6") {
    suitNames.push(" of Rackets");
    suitNames.push(" of Wheels");
  }

  // Create all suit stacks as an array of arrays
  var suits = [];
  for (var i = 0; i < Cd.length; i++) {
    for(var j = 0; j < suitNames.length; j++) {
        if(i===0) suits.push([]);
        suits[j].push(Cd[i] + suitNames[j]);
    }
  }

  // Combine all cards into a single array
  var P = Array.prototype.concat.apply([], suits);

  // Shuffle the deck
  for (i = 0; i < P.length; i++) {
    var Q = Choose(P);
    R = P.indexOf(Q);
    Result[i] = (i + 1) + ": " + Q;
    P = P.slice(0, R).concat(P.slice(R + 1));
  }

  document.getElementById("Cards").innerHTML = Result.join("\n");

}

这是一个正在运行的示例: https://jsfiddle.net/b4rf3ne2/

这更接近你正在寻找的东西吗?

答案 3 :(得分:0)

使用ES6我认为我们可以进行微小的改进。

let {S, H, C, D} = Cd.reduce((state, cardType, idx) => {
  state.S.push(cardType + ' of Spades');
  state.H.push(cardType + ' of Hearts');
  state.C.push(cardType + ' of Clubs');
  state.C.push(cardType + ' of Diamonds');
  return state;   
}, {
  S: [],
  H: [],
  C: [],
  D: [],
});