js對象所有屬性轉Map

來源:互聯網
上載者:User

標籤:val   $scope   sem   參數   lang   nbsp   this   htm   賦值   

  1 <!DOCTYPE html>  2 <html lang="zh_CN">  3 <head>  4     <meta charset="UTF-8">  5     <title>Angular基礎-自訂過濾器</title>  6 </head>  7 <body>  8 <div ng-app="myApp">  9     <div ng-controller="myCtrl"> 10         <h2>ng-repeat 求和</h2> 11         <table border="1" width="400"> 12             <th> 13             <td>姓名</td> 14             <td>年齡</td> 15             <td>身高</td> 16             </th> 17             <tr ng-repeat="item in items"> 18                 <td>{{$index+1}}</td> 19                 <td>{{item.name}}</td> 20                 <td>{{item.age}}</td> 21                 <td>{{item.stature}}</td> 22             </tr> 23             <tr> 24                 <td>合計</td> 25                 <td></td> 26                 <td ng-bind="items | sumAge:items:‘age‘"></td> 27                 <td></td> 28             </tr> 29         </table> 30     </div> 31  32 </div> 33 <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> 34 <script type="application/javascript"> 35     var myApp = angular.module(‘myApp‘, [‘myApp.filter‘]); 36     var appFilter = angular.module(‘myApp.filter‘, []); 37     //自訂過濾器 38     myApp.filter(‘sumAge‘, function () { 39         //根據第一個參數,第二個參數求和 40         return function (input, n1, n2) { 41             // console.log("輸入值 : "+input); 42             console.log("第一個參數: " + n1); 43             console.log("第二個參數 :" + n2); 44             var sum = 0; 45             allPrpos(n1[0]); 46  47             /* for (var int = 0; int < input.length; int++) { 48              sum += parseFloat(input[int].n2); 49              };*/ 50             return sum; 51         } 52     }); 53     function allPrpos(obj) { 54         // 用來儲存所有的屬性名稱和值的Map 55         var propsMap = new Map(); 56         // 開始遍曆 57         for (var p in obj) { // 屬性為方法 58             if (typeof ( obj [p]) == " function ") { 59                 obj [p](); 60             } else { // p 為屬性名稱,obj[p]為對應屬性的值 61                 propsMap.put(p,obj[p]); 62             } 63         } 64         // 最後顯示所有的屬性 65         console.log(propsMap); 66     } 67  68     //封裝http請求索引值對的函數 69     function Map() { 70         this.keys = new Array(); 71         this.data = {}; 72         //添加索引值對 73         this.put = function(key, value) { 74             if (this.data[key] == null) { //如鍵不存在則給鍵域數組添加鍵名 75                 this.keys.push(key); 76             } 77             this.data[key] = value; //給鍵索引對應的範圍賦值 78         }; 79         //擷取鍵對應的值 80         this.get = function(key) { 81             return this.data[key]; 82         }; 83         //去除索引值,(去除鍵資料中的鍵名及對應的值) 84         this.remove = function(key) { 85             this.keys.remove(key); 86             this.data[key] = null; 87         }; 88         //判斷索引值元素是否為空白 89         this.isEmpty = function() { 90             return this.keys.length == 0; 91         }; 92         //擷取索引值元素大小 93         this.size = function() { 94             return this.keys.length; 95         }; 96     } 97  98 </script> 99 <script>100     myApp.controller(‘myCtrl‘, [‘$scope‘, function ($scope) {101         $scope.items = [102             {103                 name: ‘魯迅‘,104                 age: 27,105                 stature: 165106             },107             {108                 name: ‘胡適‘,109                 age: 25,110                 stature: 170111             },112             {113                 name: ‘契訶夫‘,114                 age: 27,115                 stature: 175116             },117             {118                 name: ‘巴爾紮克‘,119                 age: 29,120                 stature: 165121             }];122     }]);123 </script>124 </body>125 </html>

 

js對象所有屬性轉Map

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.