将属性对象转换为其值

时间:2017-04-11 10:32:52

标签: javascript arrays object

让我们说,一方面我有一个对象,另一方面是一个具有该对象的一些属性的数组。 如何将该属性数组转换为通过函数传递它的值,而无需在我的函数中手动引入引号"转换"?

var blackjack = {
  "A": 1 ,
  "2": 2,
  "3": 3,
  "4": 4,
  "5": 5,
  "6": 6,
  "7": 7,
  "8": 8,
  "9": 9,
  "10":10,
  "J": 10,
  "Q": 10,
  "K": 10
  }


var reparto=function(){
var repart=[];

for (var i=0;i<1;i++)
{
var randomchoice = Math.random();
// probabilidad de cada carta es de 1/13
if(randomchoice<=0.0769231){
    repart.push("A")
}else if(randomchoice> 0.0769231 && randomchoice<=0.0769231*2){
    repart.push(2)
}else if(randomchoice>0.0769231*2 && randomchoice <=0.0769231*3){
    repart.push(3)
  }else if(randomchoice> 0.0769231*3 && randomchoice<=0.0769231*4){
      repart.push(4)
  }else if(randomchoice>0.0769231*4 && randomchoice <=0.0769231*5){
      repart.push(5)
    }else if(randomchoice> 0.0769231*5 && randomchoice<=0.0769231*6){
        repart.push(6)
    }else if(randomchoice>0.0769231*6 && randomchoice <=0.0769231*7){
        repart.push(7)
      }else if(randomchoice> 0.0769231*7 && randomchoice<=0.0769231*8){
          repart.push(8)
      }else if(randomchoice>0.0769231*8 && randomchoice <=0.0769231*9){
          repart.push(9)
        }else if(randomchoice> 0.0769231*9 && randomchoice<=0.0769231*10){
            repart.push(10)
        }else if(randomchoice>0.0769231*10 && randomchoice <=0.0769231*11){
            repart.push("J")
          }else if(randomchoice>0.0769231*11 && randomchoice <=0.0769231*12)
     {
              repart.push("Q")
            }else if(randomchoice>0.0769231*12){
              repart.push("K")
            }

          }

        return repart;
     } 


  var jugar = document.getElementById('start')
  var mas = document.getElementById('hit')
  var mantener = document.getElementById('stand')
  var cuerpo= document.getElementsByTagName('body')[0]
  var contador_universal_cli=[]
  var contador_universal_dea=[]
  var recuadro_cliente = document.createElement('div');
  recuadro_cliente.setAttribute('class','tabla_cliente');

// EVENT
  jugar.onclick= function(){
  for (var i = 0; i<4;i++){
  var card = document.createElement('div');
  var rdo= reparto()
  var texto =document.createTextNode(rdo);
  // var texto =document.createTextNode(reparto());
  card.appendChild(texto);
  cuerpo.appendChild(card);
  if(i%2==0){
  //como solucionar esto para aplicar las clases?
  card.setAttribute("class","cartas_dea")
  contador_universal_cli.push(rdo);
  // contador_universal_cli.push(reparto());
  }
  else{
  card.setAttribute("class","cartas_cli")
  contador_universal_dea.push(rdo);
  // contador_universal_dea.push(reparto());
    }

    }
  //  console.log(contador_universal_cli+" "+contador_universal_dea)

    }

阵列将是:&#34; contador_universal_cli&#34;

1 个答案:

答案 0 :(得分:1)

您可以使用Array#map()

您需要在数组arr中添加引号,否则KJ将被解释为变量

var blackjack = {
  "A": 1,
  "2": 2,
  "3": 3,
  "4": 4,
  "5": 5,
  "6": 6,
  "7": 7,
  "8": 8,
  "9": 9,
  "10": 10,
  "J": 10,
  "Q": 10,
  "K": 10
};


var arr = [8, 9, 'K', 'J'];

console.log(arr.map(a=>blackjack[a]));

如果您不想在数组中使用引号,可以使用以下解决方案Array.from(..)

var blackjack = {
  "A": 1,
  "2": 2,
  "3": 3,
  "4": 4,
  "5": 5,
  "6": 6,
  "7": 7,
  "8": 8,
  "9": 9,
  "10": 10,
  "J": 10,
  "Q": 10,
  "K": 10
};

var arr = Array.from('89KJ');

console.log(arr.map(a=>blackjack[a]));