Created
April 1, 2014 13:32
-
-
Save sourcec0de/9914035 to your computer and use it in GitHub Desktop.
Select keys from a javascript object using a regular expression. Returns an object containing only the keys that matched the expression in the original object.
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
var keyMatch = function(o,r){ | |
var c = 0; | |
var nO = {}; | |
Object.keys(o).forEach(function(k){ | |
c++; | |
no[k] = k.match(r) ? o[k] : void 0; | |
}); | |
return ( ~c ? JSON.stringify(JSON.parse(nO)) : null ); | |
}; | |
var test = { | |
abc: 'foo', | |
acb: 'foo', | |
bac: 'bar' | |
}; | |
keyMatch(test,/^a/); | |
// Returns | |
// =============== | |
// { | |
// abc: 'foo', | |
// acb: 'foo' | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It did not work.