Méthode d'implémentation de la fonction JS pour déterminer si un tableau contient un certain élément
Array.prototype.contains = fonction (obj) {var i = this.length; while (i--) {if (this [i] === obj) {return true; }} return false;}ou
Array.prototype.Contains = fonction (élément) {for (var i = 0; i <this.length; i ++) {if (this [i] == élément) {return true; }} return false;}ou
Array.prototype.in_array = fonction (e) {for (i = 0; i <this.length && this [i]! = E; i ++); retour! (i == this.length);}Un autre grand taureau a écrit ceci:
Array.prototype.s = string.fromCharcode (2); array.prototype.in_array = fonction (e) {var r = new regexp (this.s + e + this.s); return (r.test (this.s + this.join (this.s) + this.s));}Comment l'utiliser est:
var arr = ["a", "b"];
alert (arr.in_array ("a"))
On dit que si itérer est la méthode la plus rapide de JS. Je ne sais pas si c'est vrai.
http://stackoverflow.com/questions/237104/javascript-array-contivainobj
La discussion ici est très intense, il est recommandé de jeter un œil. Si vous utilisez jQuery, vous pouvez utiliser directement JQuery pour l'implémenter. Adresse de référence:
http://api.jquery.com/jquery.inarray/
L'article ci-dessus pour déterminer si un tableau contient la méthode d'implémentation de la fonction JS d'un certain élément est tout le contenu que j'ai partagé avec vous. J'espère que vous pourrez vous faire référence et j'espère que vous pourrez soutenir Wulin.com plus.