首页 > 代码库 > js对象所有属性转Map

js对象所有属性转Map

  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 =http://www.mamicode.com/ {};
 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