Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Created December 10, 2012 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benfoxall/4250358 to your computer and use it in GitHub Desktop.
Save benfoxall/4250358 to your computer and use it in GitHub Desktop.
function sampled(arr, size){
if(arr.length <= size){
return arr
} else {
var sampled = [];
for(var i = 0; i < size; i++){
var idx = Math.ceil((i / size) * arr.length);
sampled[i] = arr[idx];
}
return sampled;
}
}
var letters = 'a b c d e f g h i j k l m n o p'.split(' ');
sampled(letters, 5); // a e h k n
sampled(letters, 10); // a c e f h i k m n p
sampled(letters, 100); // a b c d e f g h i j k l m n o p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment