You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

数组克隆异常:从ArrayList复制数组时出现TypeError未定义错误

问题描述

我尝试将一个ArrayList中的元素克隆到另一个ArrayList中,但运行后出现异常输出。试过用Object.assign()函数赋值,也试过迭代器方法,都没能解决问题。

代码片段

await (()=>{
    for (let i = 0; i < 5; i++) {
        const cardID = Math.floor(Math.random() * 100) + 1
        this.cards[i + 1] = this.deck[cardID];
        this.positions[i] = {
            x: start + this.offest,
            y: 50,
            card: this.deck[cardID]
        }
        delete this.deck[cardID];
        start += this.offest;
    }})();

start = 350;
await (()=>{
    for (let i = 6; i < 11; i++) {
        const cardID = Math.floor(Math.random() * 100) + 1
        this.cards.push(this.deck[cardID]);
        this.positions[i] = {
            x: start + this.offest,
            y: 200,
            card: this.deck[cardID],
            
        }
        delete this.deck[cardID];
        start += this.offest;
    }})();


/** this.deck = this.deck.filter(function (el){
    return el != null;
})
**/
setTimeout(()=>{
    
   this.positions.map((v,i,a)=>{
        
        let c = v["card"];
        c["index"] = i
        let animation = this.animator.Animation.new(c);
        animation.moveTo(v.x,v.y,50,10);
    
        console.log(this.positions.length)
    })
    
},500)
this.hasAnimated = true;
}else{
    if(!this.inProgress && this.hasAnimated && Object.entries(this.animator.animations).length != 10 || Object.entries(this.animator.completed).length == 10){
        if(this.deckIndex >= 1 && this.table.length > 1){
            delete this.table[this.table.length - 1]
        }
        this.animator.clear();
        console.log(this.table);
        console.log(this.deck[this.deckIndex])
        this.deck[this.deckIndex].hasShown = true;
        this.table.push(this.deck[this.deckIndex]);
        console.log(this.table);
        
        let animation = this.animator.Animation.new(this.deck[this.deckIndex]);
        animation.moveTo(350,500,50,10);
      
        this.inProgress = true;

运行输出

[]
game.js:113 
[]
game.js:114 
Card {x: 50, y: 50, width: 50, height: 50, input: Input, …}
game.js:131 
[Card]
game.js:134 Uncaught (in promise) 
TypeError: Cannot read properties of undefined (reading 'id')
    at game.js:134:47

内容的提问来源于stack exchange,提问作者Invisio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:25:21