[jQuery] $.each & $.map

  • $.each
    • json
      • 1 var jsonArray = [ { "name": "joke", "age": 21 }, { "name": "marry","age": 23} ];
        2 $.each(jsonArray,function (index,obj) {
        3     alert(jsonArray[index].name);
        4     alert(obj.name);
        5 });
      • 结果:joke joke marry marry
    • map
      • 1 var map = {1: "haha", 2: "hehe", 3: "xixi"};
        2 $.each(map,function (key,value) {
        3     alert(key);
        4     alert(value);
        5 });
      • 结果:1 haha 2 hehe 3 xixi
    • array
      • 1 var array = ["one", "two", "three"];
        2 $.each(array,function (index,value) {
        3     alert(index);
        4     alert(value);
        5 });
      • 结果:0 one 1 two 2 three
  • $.map
    • json
      • 1 var jsonArray = [ { "name": "joke", "age": 21 }, { "name": "marry","age": 23} ];
        2 $.map(jsonArray,function (obj,index) {
        3     alert(obj.age);
        4     alert(index);
        5 });
      • 结果:21 0 23 1
    • map
      • 1 var map = {1: "haha", 2: "hehe", 3: "xixi"};
        2 $.map(map,function (value,key) {
        3     alert(value);
        4     alert(key);
        5 });
      • 结果:haha 1 hehe 2 xixi 3
    • array
      • 1 var array = ["one", "two", "three"];
        2 $.map(array,function (value,index) {
        3     alert(value);
        4     alert(index);
        5 });
      • 结果:one 0 two 1 three 2

 

  • 获取checkbox值应用
    • 1 var checkedValue = $(‘input:checkbox:checked‘).map(function () {
      2     return $(this).val();
      3 }).get().join(‘,‘);
      4 alert(checkedValue);

       

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。