This can also be done using a prototype array.
Array.prototype.containsByProp = function(propName, value){ for (var i = this.length - 1; i > -1; i--) { var propObj = this[i]; if(propObj[propName] === value) { return true; } } return false; } var myArr = [ { name: "lars", age: 25 }, { name: "hugo", age: 28 }, { name: "bent", age: 24 }, { name: "jimmy", age: 22 } ]; console.log(myArr.containsByProp("name", "brent"));
The code can also be found and tested here.
Simon pertersen
source share