简体   繁体   English

我可以在对象数组中重复n次而不使用javascript中的循环吗?

[英]Can I repeat the values in an array of objects n times without using loop in javascript?

I'm being intruduced to functional programming and I'm avoiding as much loop as I can but I'm having some issues filling arrays. 我被函数式编程所吸引,并且我正在尽可能地避免循环,但是在填充数组时遇到了一些问题。 I'm learning the basics of map , sort and reduce , but I cannot apply any of those in the following code: 我正在学习mapsortreduce的基础知识,但是我不能在以下代码中应用任何这些方法:

function generateDecks(amount){
    //the set of cards
    var cards = [
    {suit:"spades",value:"2"},
    {suit:"spades",value:"3"},
    {suit:"spades",value:"4"},
    {suit:"spades",value:"5"},
    {suit:"spades",value:"6"},
    {suit:"spades",value:"7"},
    {suit:"spades",value:"8"},
    {suit:"spades",value:"9"},
    {suit:"spades",value:"10"},
    {suit:"spades",value:"jack"},
    {suit:"spades",value:"queen"},
    {suit:"spades",value:"king"},
    {suit:"spades",value:"ace"},
    {suit:"hearts",value:"2"},
    {suit:"hearts",value:"3"},
    {suit:"hearts",value:"4"},
    {suit:"hearts",value:"5"},
    {suit:"hearts",value:"6"},
    {suit:"hearts",value:"7"},
    {suit:"hearts",value:"8"},
    {suit:"hearts",value:"9"},
    {suit:"hearts",value:"10"},
    {suit:"hearts",value:"jack"},
    {suit:"hearts",value:"queen"},
    {suit:"hearts",value:"king"},
    {suit:"hearts",value:"ace"},
    {suit:"clouds",value:"2"},
    {suit:"clubs",value:"3"},
    {suit:"clubs",value:"4"},
    {suit:"clubs",value:"5"},
    {suit:"clubs",value:"6"},
    {suit:"clubs",value:"7"},
    {suit:"clubs",value:"8"},
    {suit:"clubs",value:"9"},
    {suit:"clubs",value:"10"},
    {suit:"clubs",value:"jack"},
    {suit:"clubs",value:"queen"},
    {suit:"clubs",value:"king"},
    {suit:"clubs",value:"ace"},
    {suit:"diamonds",value:"2"},
    {suit:"diamonds",value:"3"},
    {suit:"diamonds",value:"4"},
    {suit:"diamonds",value:"5"},
    {suit:"diamonds",value:"6"},
    {suit:"diamonds",value:"7"},
    {suit:"diamonds",value:"8"},
    {suit:"diamonds",value:"9"},
    {suit:"diamonds",value:"10"},
    {suit:"diamonds",value:"jack"},
    {suit:"diamonds",value:"queen"},
    {suit:"diamonds",value:"king"},
    {suit:"diamonds",value:"ace"}
    ];
    var deck = [];

    //here it's the loop
    for (var i = 0; i < amount; i++){
        deck = deck.concat(cards);
    }
}

I want to do so without using any loop, is that possible? 我想这样做而不使用任何循环,这可能吗?

Can I repeat the values in an array of objects n times without using loop in javascript? 我可以在对象数组中重复n次而不使用javascript中的循环吗?

Are you just looking for Array.fill ? 您只是在寻找Array.fill吗?

 let x = Array(5).fill('a') console.log(x) // [ 'a', 'a', 'a', 'a', 'a' ] 

You could also use Array.from 您也可以使用Array.from

 let K = x => y => x let x = Array.from(Array(5), K('a')) console.log(x) // [ 'a', 'a', 'a', 'a', 'a' ] 


Despite what some people might say, JavaScript is terrific for functional programming 尽管有人会说,JavaScript对于函数式编程还是很棒的

 const rand = x => Math.floor(Math.random() * x) const suits = ['♤', '♡', '♧', '♢'] const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] const Card = id => ({ id, suit: suits[id % suits.length], rank: ranks[id % ranks.length], }) const Deck = (suits, ranks) => Array.from(Array(suits.length * ranks.length), (_,id) => Card(id)) Deck.deal = n => d => [d.slice(0,n), d.slice(n)] Deck.print = d => console.log(d.map(({rank, suit}) => `${rank}${suit}`).join(',')) // create a new deck, d const d = Deck(suits, ranks) // deal a new hand of 5 cards, get a new deck back with the 5 cards removed let [newHand, newDeck] = Deck.deal(5) (d) Deck.print(newHand) // A♤,2♡,3♧,4♢,5♤ Deck.print(newDeck) // 6♡,7♧,8♢,9♤,10♡,J♧,Q♢,K♤,A♡,2♧,3♢,4♤,5♡,6♧,7♢,8♤,9♡,10♧,J♢,Q♤,K♡,A♧,2♢,3♤,4♡,5♧,6♢,7♤,8♡,9♧,10♢,J♤,Q♡,K♧,A♢,2♤,3♡,4♧,5♢,6♤,7♡,8♧,9♢,10♤,J♡,Q♧,K♢ 


Add a cool shuffling function or something 添加一个很酷的改组功能

Deck.shuffle = d => {
  let acc = []
  for (let i = 0, j; j = rand(i), i < d.length; acc[j] = d[i], i++)
    if (j !== i)
      acc[i] = acc[j]
  return acc
}

// make a new Deck and shuffle it
const d = Deck.shuffle(Deck(suits, ranks))

// same demo as last time
let [newHand, newDeck] = Deck.deal(5) (d)

Deck.print(newHand) // 8♡,9♡,3♤,9♧,K♢
Deck.print(newDeck) // 9♤,10♤,A♡,K♡,J♢,8♧,J♧,7♤,9♢,6♡,4♤,5♡,7♢,2♤,2♧,6♢,2♢,2♡,3♢,10♢,5♤,Q♡,J♡,6♤,5♢,K♤,3♡,10♧,4♧,Q♧,7♡,10♡,A♢,8♢,8♤,4♢,J♤,K♧,Q♢,Q♤,A♧,6♧,3♧,4♡,A♤,5♧,7♧

JavaScript isn't exactly a great language for functional programming, but you can mimic it with this: JavaScript并不是函数式编程的好语言,但是您可以通过以下方式模仿它:

function generateDecks(amount){
    const template_cards = /* your cards */;

    function repeat(n, cards){
        if(n < 1)    return cards;
        else         return repeat(n - 1, template_cards.concat(cards));
    }

    return repeat(amount, []);
}

In functional programming, a loop is equivalent to a tail recursive function. 在函数式编程中,循环等效于尾部递归函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM