Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexpilugin/b1ed0330ac0ce308d31774a3b0225fd8 to your computer and use it in GitHub Desktop.
Save alexpilugin/b1ed0330ac0ce308d31774a3b0225fd8 to your computer and use it in GitHub Desktop.
var myArray = [{ 'id': '73', 'foo': 'one' }, { 'id': '45', 'foo': 'two' }];
function searchByPropValue(prop, value) {
return function (element) {
if (element.hasOwnProperty(prop)) {
if (value) {
if (element[prop] == value) return element;
} else {
return element;
}
}
}
};
myArray.find(searchByPropValue('id')); //first found (matching) element
myArray.find(searchByPropValue('foo', 'two'));
myArray.find(searchByPropValue('loo')); //unefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment