使用变量作为对象键

时间:2019-02-22 01:30:54

标签: javascript

我希望将变量插入对象键结构中,但我只是获得变量名称而不是变量值。

var winnerId = 10;
var loserId = 11;        

newMessage.setMessageData({
  winnerId : {
    "status" : "win",
    "choice" : playerData[winnerId]["currentChoice"],
    "newScore" : playerData[winnerId]["score"]
  },
  loserId : {
    "status" : "lost",
    "choice" : playerData[loserId]["currentChoice"],
    "newScore" : playerData[loserId]["score"]
  }           
});

1 个答案:

答案 0 :(得分:4)

您应该使用computed-property-names

<div id="nav_box">
  <!--nav box open-->
  <div id="box1">
    <img class="lilylink l1" src="lilypad.png" alt="lilypad 1">
    <div class="overlay">
      <span class="hovering">Hello!!!</span>
    </div>
  </div>

  <div id="box2">
    <img class="lilylink l2" src="lilypad.png" alt="lilypad 2">
    <div class="overlay">
      <span class="hovering">Hello 2</span>
    </div>
  </div>

  <div id="box3">
    <img class="lilylink l1" src="lilypad.png" alt="lilypad 3">
    <div class="overlay">
      <span class="hovering">Hello 3</span>
    </div>
  </div>


  <div id="box4">
    <img class="lilyink l2" src="lilypad.png" alt="lilypad 4">
    <div class="overlay">
      <span class="hovering">Hello 4</span>
    </div>
  </div>


</div>
<!--nav box close-->