首页 > 代码库 > JS遍历对象或者数组

JS遍历对象或者数组

一.纯js实现

<script>    var obj = {"player_id":"GS001","event_id":"1","destroy":"97%"};    var props = "";    for(var p in obj){        if(typeof(obj[p])=="function"){              obj[p]();        }else{              props+= p + "=" + obj[p] + " ";           }    }    alert(props);  </script> 

二.jquery实现

1.遍历对象

$(function(){         var tbody = "";          //------------遍历对象 .each的使用-------------        //对象语法JSON数据格式(当服务器端回调回来的对象数据格式是json数据格式,必须保证JSON的格式要求,回调的对象必须使用eval函数进行转化(否则将得不到Object)。本文不作详细介绍服务器端回调的数据问题,我们将直接自定义对象)      var obj =[{"name":"admin","password":"123456"}];      $("#result").html("------------遍历对象 .each的使用-------------");        alert(obj);//是个object元素      //下面使用each进行遍历      $.each(obj,function(n,value) {              alert(n+‘ ‘+value);             var trs = "";               trs += "<tr><td>" + value.name +"</td> <td>" + value.password +"</td></tr>";               tbody += trs;                    });             $("#project").append(tbody);           }); 

2.遍历数组

$(function(){     var tbody = "";     //------------遍历数组 .each的使用-------------     var anArray = [‘one‘,‘two‘,‘three‘];     $("#result").html("------------遍历数组 .each的使用-------------");           $.each(anArray,function(n,value) {             alert(n+‘ ‘+value);             var trs = "";             trs += "<tr><td>" +value+"</td></tr>";             tbody += trs;           });          $("#project").append(tbody);       }); 

3.遍历List集合

$(function(){    var tbody = "";    //------------遍历List集合 .each的使用-------------    var obj =[{"name":"zhangsan","password":"123456"},{"name":"lisi","password":"333333"}];    $("#result").html("遍历List集合 .each的使用");    alert(obj);//是个object元素    //下面使用each进行遍历    $.each(obj,function(n,value) {        alert(n+‘ ‘+value);       var trs = "";       trs += "<tr><td>" +value.name+"</td> <td>" + value.password +"</td></tr>";       tbody += trs;           });    $("#project").append(tbody);  }); 

一个完整的例子,向一个select添加元素

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">  <head>      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />      <script type="text/javascript" src="http://www.mamicode.com/js/jquery-1.6.js"></script>  </head>    <body>  <script type="text/javascript">      var cities = new Array({"id":1,"name":"nanjing"},{"id":2,"name":"shanghai"});      function add(){          var added = "";          $.each(cities,function(n, value){              //n表示下标              var str = "";              str = ‘<option id=‘ + value.id + ‘ value=http://www.mamicode.com/‘+ ‘"‘ + value.name +  ‘">‘ + value.name + ‘</option>‘ + ‘<br/>‘;              added += str;          });          //alert(added);          $("#selector").append(added);      }        function add2(){          var added = ‘‘;          for(var i = 0; i < cities.length; i++ ){              var datahttp://www.mamicode.com/= "";              for(var k in cities[i]){                if(typeof(cities[i][k])=="function"){                     cities[i][k]();                  }else{                    data +=  k + "=" +  cities[i][k] + ‘ ‘;                   }               }              added += data;          }          var select = document.getElementById("test");          select.innerHTML = added;      }      $(function(){          $("#add").click(function(){              add();          });      });  </script>      <button id="add">显示select</button><br/><br/>      <select id="selector">      </select>      <div id="test">      </div>    </body>  

 

JS遍历对象或者数组