Last active
February 1, 2019 11:24
-
-
Save alexpilugin/b1ed0330ac0ce308d31774a3b0225fd8 to your computer and use it in GitHub Desktop.
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 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