zoukankan      html  css  js  c++  java
  • js 判断数组包含某值的方法 和 javascript数组扩展indexOf()方法

    var  questionId = []; var anSwerIdValue = [];

    ////javascript数组扩展indexOf()方法

    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("请你答满13题目");
    return false;
    } else if (questionId.length == 13 && anSwerIdValue.indexOf(questionId[12] + '|') != -1) {

    $("#pagingForm").attr("action", "@Url.Action("QuestionAtion", "QuestionSubmit")");
    //form.submit();
    document.getElementById("pagingForm").submit();
    } else {
    alert("请你答满13题目");
    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. questionIdArray[13]
      1. 0"14"
      2. 1"15"
      3. 2"16"
      4. 3"17"
      5. 4"18"
      6. 5"19"
      7. 6"20"
      8. 7"21"
      9. 8"22"
      10. 9"23"
      11. 10"24"
      12. 11"25"
      13. 12"26"
      14. length13

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

    /////js 判断数组包含某值的方法

    //// 判断数组中包含element元素

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

    这样就可以直接调用此方法来判断。

    var arr = new Array();

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

    arr[i] = "dfsdfsd";

    }

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

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

    实例2:javascript版本 Array.prototype.indexOf 与 Array.prototype.lastIndexOf 方法扩展

    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;
    }

    然后lz可以这样:
    var a =['red','blue','green','blue'];
    alert(a.indexOf("blue"));
    alert(a.lastIndexOf("blue"));

  • 相关阅读:
    免费的视频、音频转文本
    Errors are values
    Codebase Refactoring (with help from Go)
    Golang中的坑二
    Cleaner, more elegant, and wrong(msdn blog)
    Cleaner, more elegant, and wrong(翻译)
    Cleaner, more elegant, and harder to recognize(翻译)
    vue控制父子组件渲染顺序
    computed 和 watch 组合使用,监听数据全局数据状态
    webstorm破解方法
  • 原文地址:https://www.cnblogs.com/mbtq/p/4147233.html
Copyright © 2011-2022 走看看