Skip to content

Instantly share code, notes, and snippets.

@yairEO
Created November 26, 2019 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yairEO/f75de80b147c81fd688ffd323f8ad6a2 to your computer and use it in GitHub Desktop.
Save yairEO/f75de80b147c81fd688ffd323f8ad6a2 to your computer and use it in GitHub Desktop.
Generate javascript array with random strings
[...Array(10)].map(() => {
const randomStr = "abcdefghijklmnopqrstuvwxyz".split('').sort(() => .5-Math.random()).join('');
return randomStr.slice(0, Math.random()*26 + 2)
})
@yairEO
Copy link
Author

yairEO commented Nov 26, 2019

Another way I've found:

Array.apply(null, Array(10)).map(function() { 
  return Array.apply(null, Array(~~(Math.random() * 10  + 3))).map(function() {
    return String.fromCharCode(Math.random() * (123 - 97) + 97); 
  }).join('') 
});

ES2015

[...Array(10)].map(() =>  
 [...Array(~~(Math.random() * 10  + 3))].map(() =>
    String.fromCharCode(Math.random() * (123 - 97) + 97)
 ).join('') 
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment