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

你好,开发人员,我想从数组中的某些对象中获取随机数据,但是当我想显示它时,它说对象[对象],而不是显示对象中的文本,我尝试了很多方法,但没有任何解决方案?非常感谢谁帮助我


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>


繁花不似锦
浏览 92回答 1
1回答

catspeake

它返回 [object] 因为文本变量是一个对象,所以如果你想获取名称,你应该通过调用它的键来获取,如first.innerHtml = text.name(获取名称)<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <meta charset="UTF-8" />&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0" />&nbsp; &nbsp; <title>Document</title>&nbsp; </head>&nbsp; <style>&nbsp; &nbsp; body {&nbsp; &nbsp; &nbsp; font-family: "Lato", sans-serif;&nbsp; &nbsp; &nbsp; display: flex;&nbsp; &nbsp; &nbsp; align-items: center;&nbsp; &nbsp; &nbsp; justify-content: center;&nbsp; &nbsp; &nbsp; flex-direction: column;&nbsp; &nbsp; }&nbsp; </style>&nbsp; <body>&nbsp; &nbsp; <h1>Get latest invoice</h1>&nbsp; &nbsp; <button class="get">Get</button>&nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Price</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Payment</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Card Number</th>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td class="first"></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="second"></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="third"></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="fourth"></td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table>&nbsp; </body>&nbsp; <script>&nbsp; &nbsp; let bill = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; name : "Sandra",&nbsp; &nbsp; &nbsp; &nbsp; price : "50$",&nbsp; &nbsp; &nbsp; &nbsp; payment : "visa",&nbsp; &nbsp; &nbsp; &nbsp; cardnumber:"1234 5678 9012 3456"&nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; name : "Johnson",&nbsp; &nbsp; &nbsp; &nbsp; price : "200$",&nbsp; &nbsp; &nbsp; &nbsp; payment : "master",&nbsp; &nbsp; &nbsp; &nbsp; cardnumber:"1234 5678 9012 3456"&nbsp; &nbsp; },&nbsp; &nbsp; ]&nbsp; &nbsp; let get&nbsp; = document.querySelector(".get");&nbsp; &nbsp; let first =&nbsp; document.querySelector(".first");&nbsp; &nbsp; let second =&nbsp; document.querySelector(".second");&nbsp; &nbsp; let third =&nbsp; document.querySelector(".third");&nbsp; &nbsp; let fourth=&nbsp; document.querySelector(".fourth");&nbsp; &nbsp; get.addEventListener("click" , ()=> {&nbsp; &nbsp; let text= bill[Math.floor(Math.random()* bill.length)];&nbsp; &nbsp; console.log(text);&nbsp; &nbsp; first.innerHTML =&nbsp; text.name;&nbsp; &nbsp; second.innerHTML = text.name;&nbsp; &nbsp; third.innerHTML =&nbsp; text.name;&nbsp; &nbsp; fourth.innerHTML =&nbsp; text.name;&nbsp; &nbsp; })&nbsp; </script></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript