Array.prototype.contains proposal

I want to propose a function for the Array.prototype similar to Array.includes, which takes a function as an argument: Array.prototype.contains.
Example implementation:

function containsFilter(){
	return this.f.call(this.context,...arguments);
}
Object.defineProperty(Array.prototype,'contains',{
	...Object.getOwnPropertyDescriptor(Array.prototype,'filter'),
	value: function contains(f){
		let cf = containsFilter.bind({context:this,f});
		return this.filter(cf).length > 0;
	}
});
// Why?
function even(n){
    return n % 2 === 0;
}
[1,2,3,4,5].contains(even)
true
[1,1,3,5,5].contains(even)
false

I think you can use Array.prototype.some() - JavaScript | MDN for this

[1,2,3].some(even); true

4 Likes

Side note, includes was going to be contains but contains couldn't be used.

1 Like