JS judgment array contains a value of the method and JavaScript array extension indexof () method

Source: Internet
Author: User
Tags javascript array

var QuestionID = []; var answeridvalue = [];

////javascript Array Extended indexOf () method

Array.prototype.indexOf = function (e) {
for (var i = 0, j; j = This[i]; i++) {
if (J.indexof (e)! =-1) {
return i;
}
}
return-1;
}

if (Answeridvalue.length < 14) {
Alert ("Please answer 13 questions");
return false;
} else if (questionid.length = = && Answeridvalue.indexof (questionid[12] + ' | ')! =-1) {

$ ("#pagingForm"). attr ("Action", "@Url. Action (" Questionation "," Questionsubmit ")");
Form.submit ();
document.getElementById ("Pagingform"). Submit ();
} else {
Alert ("Please answer 13 questions");
return false;
}

QuestionID:

-------------------------------------

$ ("Input[name=questionid]"). each (function () {

Questionid.push ($ (this). Val ());
});

Answeridvalue:

---------------------------------------------------
$ ("input[name=answeridvalue]:checked"). each (function () {
var QuestionID = $ (this). attr ("Data-questionid");
var Qtype = $ (this). attr ("Data-type");
if (Qtype = = 2) {
Answeridvalue.push (QuestionID + "|" + $ (This). Val ());
$ (this). Val (QuestionID + "|" + $ (This). Val ());
var answeritemid = [];
Answeritemid.push (QuestionID + "|" + $ (This). Val ());
$ ("#answerItemId" + QuestionID). Val (Answeritemid);
} else {
Answeridvalue.push ($ (this). Val ());
}

});

----------------------------------------------------------------

1.ANSWERIDVALUE:ARRAY[18]
0: "14|11"
1: "14|13"
2: "14|14"
3: "14|19"
4: "14|20"
5: "14|23"
6: "15|25"
7: "16|28"
8: "17|34"
9: "17|35"
10: "17|36"
11: "18|62"
12: "18|63"
13: "19|58"
14: "19|59"
15: "20|54"
16: "20|55"
17: "21|51"

----------------------------------------------------------------

  1. QuestionID: array[13]
    1. 0: "+"
    2. 1: "the "
    3. 2: "+"
    4. 3: "+"
    5. 4: "All"
    6. 5: "+"
    7. 6: " the"
    8. 7: "+"
    9. 8: "a"
    10. 9: "All"
    11. Ten: "
    12. One: " "
    13. : "+"
    14. Length:

---------------------------------------------------------

JS to determine how an array contains a value

To determine if the array contains element elements

Example 1:
Array.prototype.contains = function (Element) {

for (var i = 0; i < this.length; i++) {
if (this[i] = = Element) {
return true;
}
}
return false;
}

This allows you to call this method directly to judge.

var arr = new Array ();

if (Arr.constains ("ddd")) {

Arr[i] = "DFSDFSD";

}

http://bbs.csdn.net/topics/360007998

---------------------------------------

Instance 2:javascript version Array.prototype.indexOf and Array.prototype.lastIndexOf method extension

Http://bbs.blueidea.com/thread-2853519-1-1.html

Array.prototype.indexOf = function (e) {
for (var i=0,j; j=this[i]; i++) {
if (j==e) {return i;}
}
return-1;
}
Array.prototype.lastIndexOf = function (e) {
for (var i=this.length-1,j; j=this[i]; i--) {
if (j==e) {return i;}
}
return-1;
}

Then the LZ can do this:
var a =[' red ', ' blue ', ' green ', ' blue ';
Alert (A.indexof ("Blue"));
Alert (A.lastindexof ("Blue"));

JS judgment array contains a value of the method and JavaScript array extension indexof () method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.