从AngularJS中的数组中删除双引号

时间:2018-06-06 07:26:46

标签: javascript arrays angularjs

这是我的傻瓜:http://plnkr.co/edit/pnJ7q62eyBILTvX1f2dj?p=preview

在console.log()中,您可以看到 @Override public void buttonClicked(Item item) { doSomeStuff(); item.setButtonVisibility (Whatever you want to do true or false) ite.setTextViewVisible (Whatever you want to do true or false) myListAdapter.notifyDataSetChanged(); } 数组之后:

Update

我想这样:

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

提前感谢您的回答!!!

6 个答案:

答案 0 :(得分:2)

您可以使用Object.entriesreduce的数组方法执行此操作:

reset

答案 1 :(得分:1)

使用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);

答案 2 :(得分:0)

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



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




答案 3 :(得分:0)

您可以使用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);

答案 4 :(得分:0)

只需使用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>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

将输入更改为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;
};