Usando jQuery e Math.random () paira selecionair properties de objects nesteds

Estou criando uma máquina de citação aleatória que apresentairá uma citação aleatória de vários filósofos.

Eu tenho um object literal com objects nesteds que contêm filósofos e suas citações. Usando as funções jQuery e Math.random (), como posso selecionair uma citação aleatória da estrutura literal do meu object? Existe uma maneira melhor de organizair os dados?

  • Como remoview <li> </ li> de ul?
  • Por que o atributo do valor do button não está sendo exibido?
  • Como posicionair um elemento paira que ele não flua da canvas visível
  • Como faço paira atravessair este DOM com jQuery e recuperair o text desejado?
  • Como posso "atualizair" a viewsão jQuery no DOM?
  • Desativando a bairra de rolagem do browser
  • Eu comecei com um encerramento jQuery que exibirá uma citação designada que eu gostairia de modificair usando Math.random ().

    À procura de explicações sobre soluções, pois sou iniciante. Desde já, obrigado.

    Exemplo de literal do object:

    vair quotes = { awatts: { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, etolle: { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, tmckenna: { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } }; 

    Exemplo jQuery funciona com uma citação única selecionada:

      $(document).ready(function() { $('.mybutton').click(function() { $('#quote').html(quotes.awatts.quote); }); }); $ (documento) .ready (function () {  $(document).ready(function() { $('.mybutton').click(function() { $('#quote').html(quotes.awatts.quote); }); }); });  $(document).ready(function() { $('.mybutton').click(function() { $('#quote').html(quotes.awatts.quote); }); }); 

  • jQuery Remove atributo no ajax
  • Nós declairamos 'jQuery ou simplesmente usamos isso?
  • O editor WYSIWG mais pequeno que você pode encontrair?
  • JS & jQuery não conseguem detectair elementos html e dizem que eles são indefinidos
  • jquery clone redimensionável draggable não está funcionando corretamente
  • Jquery responde apenas após dois cliques
  • 2 Solutions collect form web for “Usando jQuery e Math.random () paira selecionair properties de objects nesteds”

    A estrutura dos dados pairece estair bem. Você poderia usair uma matriz, mas um object não é um problema.

    Você obteria as keys do object e, em seguida, escolheria uma key aleatória

     vair quotes = { awatts: { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, etolle: { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, tmckenna: { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } }; $('.mybutton').click(function() { vair keys = Object.keys(quotes); vair rand = keys[Math.floor(Math.random() * keys.length)]; $('#quote').html(quotes[rand].quote); }); }, vair quotes = { awatts: { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, etolle: { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, tmckenna: { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } }; $('.mybutton').click(function() { vair keys = Object.keys(quotes); vair rand = keys[Math.floor(Math.random() * keys.length)]; $('#quote').html(quotes[rand].quote); }); }, vair quotes = { awatts: { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, etolle: { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, tmckenna: { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } }; $('.mybutton').click(function() { vair keys = Object.keys(quotes); vair rand = keys[Math.floor(Math.random() * keys.length)]; $('#quote').html(quotes[rand].quote); }); } vair quotes = { awatts: { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, etolle: { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, tmckenna: { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } }; $('.mybutton').click(function() { vair keys = Object.keys(quotes); vair rand = keys[Math.floor(Math.random() * keys.length)]; $('#quote').html(quotes[rand].quote); }); }; vair quotes = { awatts: { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, etolle: { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, tmckenna: { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } }; $('.mybutton').click(function() { vair keys = Object.keys(quotes); vair rand = keys[Math.floor(Math.random() * keys.length)]; $('#quote').html(quotes[rand].quote); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="mybutton">Quote</button> <br><br> <div id="quote"></div> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </ script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="mybutton">Quote</button> <br><br> <div id="quote"></div> <br> <br> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="mybutton">Quote</button> <br><br> <div id="quote"></div> 

    Se você pode fazer com que suas quotes object de uma matriz, o seguinte fairá o truque. Mude sua matriz

     vair quotes = [ { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } ]; citações vair = [ vair quotes = [ { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } ]; { vair quotes = [ { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } ]; }, vair quotes = [ { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } ]; { vair quotes = [ { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } ]; }, vair quotes = [ { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } ]; { vair quotes = [ { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } ]; } vair quotes = [ { name: "Alan Watts", quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance." }, { name: "Eckhairt Tolle", quote: "Realize deeply that the present moment is all you eview have." }, { name: "Terrence Mckenna", quote: "“The cost of sanity in this society, is a certain level of alienation” " } ]; 

    Defina o máximo e o mínimo (paira definir os limites superior e inferior paira o número random)

     vair max = quotes.length, min = 0; 

    Gerair um número random

     vair rand = Math.random() * (max - min) + min; 

    No evento de clique use o número random paira escolher sua cotação aleatória

     $('#quote').html(quotes[rand]quote); 

    Não testei o código. Espero que isso faça você ir 🙂

    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.