以编程方式删除对象数组,JavaScript

Programmatically remove an object array, JavaScript

我正在尝试以编程方式记住一个对象数组,但我缺乏技能,而且我似乎也没有在网上找到答案。

因此,我希望能够在绘制 cardObj 时移除其反向(倒置)选项。请注意,如果反向版本首先是抽屉,我想做同样的事情 - 从甲板上取下直立的。

代码如下:

Cardz Exerz!

EXERZ!

过去、现在和未来
Spread

PASTPRESENTFUTURE// ---------- JAVASCRIPT----------------//
  // CARDS OBJECT ------------------------------------------------------------------------------------>
  let cardObj = [
  {name: "0_Fool", imgUrl: "data/0_Fool.jpg"},
  {name: "0_Fool_R", imgUrl: "data/0_Fool_R.jpg"}, 
  //{name: "1_Magician", imgUrl: "data/1_Magician.jpg"}, 
  //{name: "1_Magician_R", imgUrl: "data/1_Magician_R.jpg"}, 
  ];
  //--- ------------------------------------------------------------------------------------>
  
  
  //SHUFFLE CARDS ------------------------------------------------------------------------------------>
        function shuffle(array){
                let currentIndex = array.length, randomIndex;
            
                    while(currentIndex != 0){
                        randomIndex = Math.floor(Math.random() * currentIndex);
                        currentIndex--;
                
                        [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
                    }
                    return array;
                     
        };
            shuffle(cardObj);
  //--- ------------------------------------------------------------------------------------>
  
  
  //DRAW ------------------------------------------------------------------------------------>
  function drawCard(id){
  if(cardObj.length === 0){
  return;
  }
  
  
  
  let castCard = document.getElementById(id);
  
  
  let card = document.createElement('img');
  card.setAttribute('src', cardObj[0].imgUrl);
  card.setAttribute('height', '272');
  card.setAttribute('width', '185');
  card.innerHTML = cardObj[0].imgUrl;
  
  
  
  if(castCard.id === 'imgPast'){
  document.getElementById("btnPast").replaceChild(card, castCard);
  }else if(castCard.id === 'imgPresent'){
  document.getElementById("btnPresent").replaceChild(card, castCard);
  }else if(castCard.id === 'imgFuture'){
  document.getElementById("btnFuture").replaceChild(card, castCard);
  }
  
  if(cardObj[0].name === cardObj[0].name){
                    cardObj = cardObj.filter(function(f) {return f !== cardObj.name + "_R"});
       
                }
  
  cardObj.shift();
  
  return false;
  }
  //--- ----------------------------------------------------------------------------------</script></body></html>

我对你在这里的问题很困惑,但我假设你想要的是根据抽取的牌删除数组的“相反”元素。

let cardObj = [
  {name: "0_Fool", imgUrl: "data/0_Fool.jpg"},
  {name: "0_Fool_R", imgUrl: "data/0_Fool_R.jpg"}, 
  {name: "1_Magician", imgUrl: "data/1_Magician.jpg"}, 
  {name: "1_Magician_R", imgUrl: "data/1_Magician_R.jpg"}, 
];
function drawCard(id){
  if(cardObj.length === 0) return;

  // assuming that all 'reverse' cards end with '_R' and that 'id' is the 'name'
  const isReverse = id.endsWith('_R');

  // construct opposite card ID
  const oppositeCardID = isReverse ? id.substring(0, id.lastIndexOf('_')) : (id + '_R');

  // Find opposite card index in the array
  const oppositeCardIndex = cardObj.findIndex(card => card.name == oppositeCardID);

  if (oppositeCardIndex < 0) return; // opposite card not found

  // Remove opposite card from deck
  cardObj.splice(oppositeCardIndex, 1);
}

console.log("Cards before drawing:", JSON.stringify(cardObj));
console.log("Drawing card 1_Magician");
drawCard("1_Magician");
console.log("Cards after drawing:", JSON.stringify(cardObj));