Skip to content

Instantly share code, notes, and snippets.

@paularmstrong
Created September 22, 2011 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paularmstrong/1235764 to your computer and use it in GitHub Desktop.
Save paularmstrong/1235764 to your computer and use it in GitHub Desktop.
filter keys from an object
function filterKeys(obj) {
var newObj = {}, key, url, i, newArr = [],
disallowed = ['constructor', 'target', 'currentTarget', 'relatedTarget', 'srcElement', 'toElement', 'view'];
for (key in obj) {
if (obj.hasOwnProperty(key) && disallowed.indexOf(key) === -1) {
if (Array.isArray(obj[key])) {
newArr = [];
i = obj[key].length;
while (i) {
i -= 1;
if (typeof obj[key][i] === 'object') {
newArr.push(filterKeys(obj[key][i]));
} else {
newArr.push(obj[key][i]);
}
}
newObj[key] = newArr;
} else if (typeof obj[key] === 'object') {
newObj[key] = filterKeys(obj[key]);
} else {
newObj[key] = obj[key];
}
}
}
return newObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment