Home >Web Front-end >JS Tutorial >javascript function to determine whether an array contains an element_javascript skills

javascript function to determine whether an array contains an element_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:26:051030browse
Copy code The code is as follows:

Array.prototype.contains = function(obj) {
var i = this.length;
while (i–) {
if (this[i] === obj) {
return true;
}
}
return false;
}

or
Copy the code The code is as follows:

Array.prototype.contains = function (element) {
for (var i = 0; i < this.length; i ) {
if (this[i] == element) {
return true;
}
}
return false;
}

It is said that while minus iteration is the fastest method in js, I don’t know if it is true.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn