Created
August 7, 2019 08:01
-
-
Save masaquid/e256a5485181f1a270f82f155d3304a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.sample = function(n) { | |
return Array.from(this).sort(_ => Math.random() - 0.5).splice(0, n > this.length ? this.length : n||1) | |
} | |
let hoge = [1,2,3,4,5]; | |
console.log(hoge.sample(3)); | |
console.log(hoge.sample(6)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TwitterでJavascriptにrubyの.sampleって無いの?って話が出てたので書いてみました
やり始めたからには、ちゃんとした実装をしたかったが 本家rubyの.sampleとはだいぶ違うものに…
色々と修正できそうですが1行で実装するのは難しそうなので、ここで終わりにしました