Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Is there a way in javascript to retrieve random objects from an array and assign them randomly to a divny clicking a button?

I want to get a random quote from an array of objects with quotes with their respective author name by clicking a button. So far I am in a roadblock and can't seem to understand how to proceed. Below is the markup Get a random code with the click of a button!


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

John Doe

GET A NEW QUOTE!

<script type="text/javascript">
const allQuotes = [

{   
qid: 1,
quote: "Quote 1",
auth: "ABC DEF"
},

{       
qid: 2,
quote: "Quote 2",
auth: "XYZ ABC"
},

{       
qid: 3,
quote: "Quote 3",
auth: "XYZ ABC"
},

{       
qid: 4,
quote: "Quote 4",
auth: "XYZ ABC"
},

{       
qid: 5,
quote: "Quote 5",
auth: "XYZ ABC"
},

{   
qid: 6,
quote: "Quote 6",
auth: "XYZ ABC"
}
</script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const allQuotes = [{qid: 1, quote: "Quote 1", auth: "ABC DEF"}, {qid: 2, quote: "Quote 2", auth: "XYZ ABC"}, {
  qid: 3,
  quote: "Quote 3",
  auth: "XYZ ABC"
}, {qid: 4, quote: "Quote 4", auth: "XYZ ABC"}, {qid: 5, quote: "Quote 5", auth: "XYZ ABC"}, {
  qid: 6,
  quote: "Quote 6",
  auth: "XYZ ABC"
}];

const randomObj = (arr = []) => {
  const num = Math.floor(Math.random() * arr.length);
  return arr[num]
};
console.log(randomObj(allQuotes));


about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!