为了账号安全,请及时绑定邮箱和手机立即绑定

从 Array Javascript 中的某些对象中获取随机项

从 Array Javascript 中的某些对象中获取随机项

繁花不似锦 2023-11-02 17:06:03
你好,开发人员,我想从数组中的某些对象中获取随机数据,但是当我想显示它时,它说对象[对象],而不是显示对象中的文本,我尝试了很多方法,但没有任何解决方案?非常感谢谁帮助我let bill = [ {   name : "Sandra",     price : "50$",     payment : "visa",     cardnumber:"1234 5678 9012 3456" },  {   name : "Johnson",     price : "200$",     payment : "master",     cardnumber:"1234 5678 9012 3456" }, ]  let get  = document.querySelector(".get"); let first =  document.querySelector(".first");  let second =  document.querySelector(".second");     let third =  document.querySelector(".third");      let fourth=  document.querySelector(".fourth");        get.addEventListener("click" , ()=> {let text= bill[Math.floor(Math.random()* bill.length)];first.innerHTML =  text;second.innerHTML = text;third.innerHTML =  text;fourth.innerHTML =  text;})body { font-family:"Lato",sans-serif;display:flex;align-items:center;justify-content:center;flex-direction:column;}<body><h1>Get latest invoice</h1><button class="get">Get</button><table >  <tr>    <th>Name</th>    <th>Price</th>     <th>Payment</th>            <th>Card Number</th>  </tr>  <tr>    <td class="first"></td>    <td class="second "></td>    <td class="third"></td>            <td class="fourth"></td>  </tr>    </table>    </body>
查看完整描述

1 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

它返回 [object] 因为文本变量是一个对象,所以如果你想获取名称,你应该通过调用它的键来获取,如first.innerHtml = text.name(获取名称)


<!DOCTYPE html>

<html>

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Document</title>

  </head>


  <style>

    body {

      font-family: "Lato", sans-serif;

      display: flex;

      align-items: center;

      justify-content: center;

      flex-direction: column;

    }

  </style>

  <body>

    <h1>Get latest invoice</h1>

    <button class="get">Get</button>

    <table>

      <tr>

        <th>Name</th>

        <th>Price</th>

        <th>Payment</th>

        <th>Card Number</th>

      </tr>

      <tr>

        <td class="first"></td>

        <td class="second"></td>

        <td class="third"></td>

        <td class="fourth"></td>

      </tr>

    </table>

  </body>


  <script>

    let bill = [

    {

      name : "Sandra",

        price : "50$",

        payment : "visa",

        cardnumber:"1234 5678 9012 3456"

    },

      {

      name : "Johnson",

        price : "200$",

        payment : "master",

        cardnumber:"1234 5678 9012 3456"

    },

    ]


    let get  = document.querySelector(".get");

    let first =  document.querySelector(".first");

    let second =  document.querySelector(".second");

    let third =  document.querySelector(".third");

    let fourth=  document.querySelector(".fourth");


    get.addEventListener("click" , ()=> {

    let text= bill[Math.floor(Math.random()* bill.length)];

    console.log(text);


    first.innerHTML =  text.name;

    second.innerHTML = text.name;

    third.innerHTML =  text.name;

    fourth.innerHTML =  text.name;


    })

  </script>

</html>


查看完整回答
反对 回复 2023-11-02
  • 1 回答
  • 0 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信