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

158
Views
Randomly return items in array based on number

I'm trying to randomly display items from an array and I've been able to get it so that one item displays at random, now I want it to display a random number of items based on a number that I put in e.g when I input 2 I want 2 random items, when I input 5 I want 5 random items, etc... How'd I go by this?

Code

const counter = 2; // Number of items I want to show
const Accounts = accountsData.sort(() => Math.random() - Math.random()).find(() => true); // This displays only one random item

<div className="text-area">
  {"Email: " + Accounts.email + " | Password: " + Accounts.password + " | Country: " + Accounts.country + " | Current Plan: " + Accounts.currentPlan + " | Has UHD: " + Accounts.hasUHD + " |  Max Streams: " + Accounts.maxStreams + " | Payment Method: " + Accounts.paymentMethod}
</div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const fetchRandomValues = (arr) => (counter) =>
  [...Array(counter)].map(() => arr[Math.floor(Math.random() * arr.length)]);

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const values = fetchRandomValues(arr)(2);
console.log(values)

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!