【问题标题】:Unable to splice array of objects无法拼接对象数组
【发布时间】:2022-11-20 03:16:00
【问题描述】:

我能够随机播放我的牌组(对象数组),但现在我正在尝试拉出/移除前 25 张牌(对象)并将它们放入自己的存储数组中。但是,当我尝试引用新牌组时,我的代码返回未定义 var player1Deck = shuffledCards.splice(25);作为非函数返回。无论如何要从我创建的这个新的洗牌数组中删除前 25 张卡片/对象?

    //Deck with ranks
   var starterDeck = [
    {"img": '2_of_clubs.png',"rank": 1},{"img": '3_of_clubs.png',"rank": 2},{"img": '4_of_clubs.png',"rank": 3},{"img": '5_of_clubs.png',"rank": 4},{"img": '6_of_clubs.png',"rank": 5},{"img": '7_of_clubs.png',"rank": 6},{"img": '8_of_clubs.png',"rank": 7},{"img": '9_of_clubs.png',"rank": 8},{"img": '10_of_clubs.png',"rank": 9},{"img": 'jack_of_clubs.png',"rank": 10},{"img": 'queen_of_clubs.png',"rank": 11},{"img": 'king_of_clubs.png',"rank": 12},{"img": 'ace_of_clubs.png',"rank": 13},    {"img": '2_of_diamonds.png',"rank": 1},{"img": '3_of_diamonds.png',"rank": 2},{"img": '4_of_diamonds.png',"rank": 3},{"img": '5_of_diamonds.png',"rank": 4},{"img": '6_of_diamonds.png',"rank": 5},{"img": '7_of_diamonds.png',"rank": 6},{"img": '8_of_diamonds.png',"rank": 7},{"img": '9_of_diamonds.png',"rank": 8},{"img": '10_of_diamonds.png',"rank": 9},{"img": 'jack_of_diamonds.png',"rank": 10},{"img": 'queen_of_diamonds.png',"rank": 11},{"img": 'king_of_diamonds.png',"rank": 12},{"img": 'ace_of_diamonds.png',"rank": 13},    {"img": '2_of_hearts.png',"rank": 1},{"img": '3_of_hearts.png',"rank": 2},{"img": '4_of_hearts.png',"rank": 3},{"img": '5_of_hearts.png',"rank": 4},{"img": '6_of_hearts.png',"rank": 5},{"img": '7_of_hearts.png',"rank": 6},{"img": '8_of_hearts.png',"rank": 7},{"img": '9_of_hearts.png',"rank": 8},{"img": '10_of_hearts.png',"rank": 9},{"img": 'jack_of_hearts.png',"rank": 10},{"img": 'queen_of_hearts.png',"rank": 11},{"img": 'king_of_hearts.png',"rank": 12},{"img": 'ace_of_hearts.png',"rank": 13},    {"img": '2_of_spades.png',"rank": 1},{"img": '3_of_spades.png',"rank": 2},{"img": '4_of_spades.png',"rank": 3},{"img": '5_of_spades.png',"rank": 4},{"img": '6_of_spades.png',"rank": 5},{"img": '7_of_spades.png',"rank": 6},{"img": '8_of_spades.png',"rank": 7},{"img": '9_of_spades.png',"rank": 8},{"img": '10_of_spades.png',"rank": 9},{"img": 'jack_of_spades.png',"rank": 10},{"img": 'queen_of_spades.png',"rank": 11},{"img": 'king_of_spades.png',"rank": 12},{"img": 'ace_of_spades.png',"rank": 13},
   ]

        for(var i=0;i<52; i++) {
            // We are taking our tempCard and placing it in the random position (randomIndex)
            var shuffledCards = starterDeck[i];
            var randomIndex = Math.floor(Math.random() * 52);
            starterDeck[i] = starterDeck[randomIndex]
            starterDeck[randomIndex] = shuffledCards;
           // let newDeck = [shuffledCards]
            console.log(shuffledCards)

            var player1Deck = shuffledCards.splice(25);
            console.log(player1Deck)
        }

【问题讨论】:

  • console.log(shuffledCards) 会得到什么?看起来你在 for 循环中将 shuffledCards 设置为一张卡片,所以它不是一个数组,因此你将无法拼接。您可能希望将处理整副牌的任何逻辑移到 for 循环之后。
  • shuffledCards = starterDeck[i] - shuffledCards 不是数组
  • 你希望shuffledCards.splice(25) 做什么?

标签: javascript


【解决方案1】:

Splice 的工作方式如下:splice(objectIndex, steps)。例如,在 [2, 3, 4, 5, 6] 的数组中,如果我执行 arr.splice(2,2) 它将删除 5 和 6。Splice 不会返回任何内容,因为它会从数组中的位置 (与返回新数组的 .map 相比,类似于 forEach 函数)。如果你想拆分甲板(前 26 个项目),请改用 .slice => slice(0, 26) 或更好的 slice(0, arr.length / 2)

【讨论】:

    猜你喜欢
    • 2020-12-25
    • 2021-06-01
    • 2019-10-16
    • 2018-12-02
    • 2017-06-11
    • 2012-02-03
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    相关资源
    最近更新 更多