简体   繁体   中英

Cant able to remove selected item from the array then push it to the new array so that the quote never repeats

https://github.com/Rishabh-Ahuja/Random-Quote-Generator

what iam trying to do is to do something that a quote when shown never repeats so i tried removing the selected / shown quote from the array and adding it to another array then after the array is undefined then again pushing all items to the original array . But iam unable to do so . PLease help

The following script shows in console quotes randomly picked up every one second. The script show a random quote only once, as after a quote has been displayed it is removed from the original quotes array.

Use Array.prototype.splice() for removing an element from an array at a specific index. Yo do not need to create a second array as mentioned in your question.

You can use this principal in your implementation, below an example:

 (function() { var arrayQ = [{ quote: "The material world is a dangerous place to be in. It's dangerous because there is a temptation of illusory energy; there is danger at every step.", source: "HH GOPAL KRISHNA GOSWAMI", citation: "ISKCON DESIRE TREE", year: 2016 }, { quote: "If you carry out the order of your spiritual master then the Lord will be so pleased that He will come to see you.", source: "HH GOPAL KRISHNA GOSWAMI", citation: "ISKCON DESIRE TREE", year: 2013 }, { quote: "Our vaccination is chanting the names of Krishna, Hearing the Bhagavad-gita and Srimad Bhagavatam, and thinking of Krishna all the time.", source: "HH GOPAL KRISHNA GOSWAMI", citation: "ISKCON DESIRE TREE", year: 2003 }, { quote: "Vairagya means that when the opportunity for sense gratification is there, I voluntarily abstain from it.", source: "HH GOPAL KRISHNA GOSWAMI", citation: "ISKCON DESIRE TREE", year: 2006 }, { quote: "Our quota is not sixteen rounds, but attentive sixteen rounds.", source: "HH GOPAL KRISHNA GOSWAMI", citation: "ISKCON DESIRE TREE", year: 2009 }, { quote: "Guru is pleased when he sees that the disciple is showing the symptoms of Shudh Bhakti.", source: "HH GOPAL KRISHNA GOSWAMI", citation: "ISKCON DESIRE TREE", year: 2010 }, { quote: "Devotional service is so simple, but devotees because of their material inclinations make it complicated.", source: "HH GOPAL KRISHNA GOSWAMI", citation: "ISKCON DESIRE TREE", year: 2011 }, { quote: "Whatever we give to Krishna that will be remembered eternally, that will be appreciated eternally.", source: "HH GOPAL KRISHNA GOSWAMI" }, ]; var randomQuotes = function() { var getRandomInt = function(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }, index = getRandomInt(0, arrayQ.length), quote = arrayQ[index]; if (quote) { console.log(quote); arrayQ.splice(index, 1);// remove quote from array } }; setInterval(function() { randomQuotes() }, 1000); })(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM