【问题标题】:Remove double quotes from array in AngularJS从AngularJS中的数组中删除双引号
【发布时间】:2018-06-06 07:26:46
【问题描述】:

这是我的小伙伴:http://plnkr.co/edit/pnJ7q62eyBILTvX1f2dj?p=preview

在console.log() 中可以看到,Update 之后的数组是这样的:

{ age : "1", weight : "1"}  

我想要这样:

{ age : 1, weight : 1}

提前感谢您的回答!!!

【问题讨论】:

    标签: javascript arrays angularjs


    【解决方案1】:

    您可以使用Object.entriesreduce 的数组方法来做到这一点:

        const obj = Object.entries({ age : "1", weight : "1"})
                     .reduce((r, v) => (r[v[0]] = +v[1], r), {});
    
        console.log(obj);

    【讨论】:

      【解决方案2】:

      使用parseInt() 将字符串值更改为整数。您也可以使用parseFloat(),但ageweight 不是浮点值,所以parseInt() 在这里更有意义。

      var obj =  {age : "1", weight : "1"};
      for(var i in obj){
        obj[i] = parseInt(obj[i]);
      }
      console.log(obj);

      基于您的plunkr 数组:

      var items = [ 
           {
            "params": {
              "age": "22",
              "weight": "66"
            }
         },
           {
            "params": {
              "age": "19",
              "weight": "54"
            }
         },
           {
            "params": {
              "age": "17",
              "weight": "75"
            }
        }
       ];
       
      items.forEach((obj)=>{
        var paramObj = obj.params;
        for(var i in paramObj){
          paramObj[i] = parseInt(paramObj[i]);
        }
      });
         
      console.log(items);

      【讨论】:

      【解决方案3】:

      您可以遍历数组并将字符串转换为数字:

      var arr = [{ age : "1", weight : "1"}, { age : "2", weight : "2"}  ];
      arr.forEach(e =>  { 
        e.age = +e.age;
        e.weight = +e.weight;
      });
      console.log(arr);

      【讨论】:

        【解决方案4】:

        您可以使用Object.keys()reduce() 创建修改后的对象:

        let obj = { age : "1", weight : "1"};
        
        let result = Object.keys(obj).reduce((a, c) => (a[c] = Number(obj[c]), a), {});
        
        console.log(result);

        【讨论】:

          【解决方案5】:

          只需使用parseInt()

          在您的 add 函数中替换以下内容

          $scope.params.age = parseInt(age);
          $scope.params.weight = parseInt(weight);
          

          var app = angular.module('plunker', []);
          
          app.controller('MainCtrl', function($scope) {
           
           $scope.items = [ 
                 {
                    "params": {
                      "age": 22,
                      "weight": 66
                    }
               },
                 {
                    "params": {
                      "age": 19,
                      "weight": 54
                    }
               },
                 {
                    "params": {
                      "age": 17,
                      "weight": 75
                    }
              }
           ]
           
            
             $scope.add = function(params , age, weight) {
          		 
          		 $scope.params = params;
          		 
          		 if(age)
                    $scope.params.age = parseInt(age);
          	   if(weight)
                    $scope.params.weight = parseInt(weight);
                    console.log($scope.params);
                  }
          
            
            $scope.update = function(){
              
            }
            
          });
          <!DOCTYPE html>
          <html ng-app="plunker">
          
            <head>
              <meta charset="utf-8" />
              <title>AngularJS Plunker</title>
              <script>document.write('<base href="' + document.location + '" />');</script>
              <link rel="stylesheet" href="style.css" />
              <script  src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
              <script src="app.js"></script>
            </head>
          
            <body ng-controller="MainCtrl">
            <div ng-repeat="n in items">
                     <ul ng-repeat="(name, param) in n.params"  style="list-style-type: none;">
                       <li>{{name}} : {{param}}</li>
                     </ul>
                 <input style="display:inline;width:130px;margin-bottom:5px;margin-top:5px;" class="form-control" name="text" placeholder="age" ng-model="age">
                 <input style="display:inline;width:115px;margin-bottom:5px;margin-top:5px;" class="form-control" name="text" placeholder="weight" ng-model="weight">
                 <br />
                 <button class="btn btn-warning" type="button" ng-click="add(n.params , age , weight)">Update</button>
                
           </div>
           
              <br />
             
            </body>
          </html>

          【讨论】:

            【解决方案6】:

            将输入更改为type="number"

               <input placeholder="age" type="number" ng-model="age">
               <input placeholder="weight" type="number" ng-model="weight">
            
               <button class="btn btn-warning" type="button" 
                       ng-click="add(n.params , age , weight)">
                 Update
               </button>
            

            或在add函数中转换为数字:

            $scope.add = function(params , age, weight) {        
                $scope.params = params;      
                age && $scope.params.age = +age;
                weight && $scope.params.weight = +weight;
            };
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-05-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-01-08
              相关资源
              最近更新 更多