1. 程式人生 > >js對象所有屬性轉Map

js對象所有屬性轉Map

pre urn name bsp input run == data prop

  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: 165 106 }, 107 { 108 name: ‘胡適‘, 109 age: 25, 110 stature: 170 111 }, 112 { 113 name: ‘契訶夫‘, 114 age: 27, 115 stature: 175 116 }, 117 { 118 name: ‘巴爾紮克‘, 119 age: 29, 120 stature: 165 121 }]; 122 }]); 123 </script> 124 </body> 125 </html>

js對象所有屬性轉Map