how to add values one by one in an array

This question already has an answer here:

  • How to append something to an array? 25 answers

  • You can try this

         <html>
          <head>
             <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
             <script>
              var arr=new Array;
              //unique object variable
              var trackObject = {};
              function reply_click(obj){
                  var id = [obj.id];
    
                    //check value is not duplicated 
                     if(!trackObject.hasOwnProperty(id)){
                        trackObject[id] = 1;
                        for(i=1;i<=id;i++){
                          arr.push(i);
                        }
                     }
                    console.log(arr);
    
              } 
              $(document).ready(function(){
                  $(".btn").click(function(){
                    var myvalues=$(this).val();
                    //check value is not duplicated
                     if(!trackObject.hasOwnProperty(myvalues)){
                        trackObject[myvalues] = 1;
                        arr.push(myvalues);
    
                      }
                      console.log(arr);
    
                  });
              });
              // attach the .equals method to Array's prototype to call it on any array
          Array.prototype.equals = function (array) {
              // if the other array is a falsy value, return
              if (!array)
                  return false;
    
              // compare lengths - can save a lot of time 
              if (this.length != array.length)
                  return false;
    
              for (var i = 0, l=this.length; i < l; i++) {
                  // Check if we have nested arrays
                  if (this[i] instanceof Array && array[i] instanceof Array) {
                      // recurse into the nested arrays
                      if (!this[i].equals(array[i]))
                          return false;       
                  }           
                  else if (this[i] != array[i]) { 
                      return false;   
                  }           
              }       
              return true;
          }  
          function compare(){
    
            result=arr.equals([1, 1, 2, 1, 2, 3, "tahirahassan", "sobia"]);
            alert(result);
          }
              </script>
              </head>
    
    
              <body >
                  <p>Click on this paragraph.</p>
                <button id="1" onClick="reply_click(this)">1</button>
              <button id="2" onClick="reply_click(this)">2</button>
               <button id="3" onClick="reply_click(this)">3</button>
    
            <button id="tahira" name="btn1" class="btn" value="tahirahassan" >hi tahira</button>
           <button id="sobia" name="btn2" class="btn" value="sobia" >hi sobia</button>
             <form action="testingjquery.php" method="post">
              <input type="submit" id="btn1" class="btn111" name="val1" value=" i am tahira"/>
              <input type="submit" id="btn2" class="btn112" name="val1" value="hassan" />
            </form>
            <button id="compare" onClick="compare()">compare</button>
    
          </body>
    
          </html>
    

    there is some useful link for you push-array and unique-value


    Use Array.prototype.push() method to add a new value at the end of the array. It will also return the new length.

    cars.push(myvalues, "myvaluesv", "Toyota") for your example. You can push any datatype into an array.

    链接地址: http://www.djcxy.com/p/17934.html

    上一篇: Javascript和尖端阵列

    下一篇: 如何在数组中逐个添加值