Skip to content

Instantly share code, notes, and snippets.

@mplungjan
Created January 5, 2013 11:21
Show Gist options
  • Save mplungjan/4461082 to your computer and use it in GitHub Desktop.
Save mplungjan/4461082 to your computer and use it in GitHub Desktop.
Filter example
var elemsToKeep = [1, 3, 5, 6, 8];
var arr=[];
arr[0] = 'one1';
arr[1] = 'two2'; // remove
arr[2] = 'three3';
arr[3] = 'four4'; // remove
arr[4] = 'five5';
arr[5] = 'six6';
arr[6] = 'seven7'; // remove
arr[7] = 'eight8';
arr[8] = 'nine9'; // remove
arr[9] = 'ten0'; // remove
function keep(element, index, array) {
var num = parseInt(element.replace(/[^\d]/g,''),10)
return elemsToKeep.indexOf(num) !=-1;
}
var filtered = arr.filter(keep);
alert(filtered)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment