This can be done very similar to how you are but with .every()
because .forEach()
always returns undefined. So you can't chain it either.
.every()
runs a method over every element like .forEach()
but if it receives a false value it will abort. Finally if every iteration returns true it will return true to the caller otherwise it will return false.
Because we return false to make it abort when a value is found (so it wont keep iterating), we then have to flip the value it returns before we return it from checkExisting
.
So using that you could do something like:
var checkExisting = function (list, obj) { return !list.every(function(elem) { return elem.text !== obj; }); }
Obviously you would have to extend that for error handling if the object doesn't have a property text etc. See fiddle: http://jsfiddle.net/reLsqhkm/ And docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every