0

Here is my plunker : http://plnkr.co/edit/pnJ7q62eyBILTvX1f2dj?p=preview

In console.log() you can see, that after Update array is like :

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

and i want like this :

{ age : 1, weight : 1}

Thanks for answers in advance!!!

bafix2203
  • 541
  • 7
  • 25

6 Answers6

2

You can use Object.entries and reduce's array method to do so:

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

    console.log(obj);
georgeawg
  • 48,608
  • 13
  • 72
  • 95
ZER0
  • 24,846
  • 5
  • 51
  • 54
1

Use parseInt() to change the string values to integer. You could also use parseFloat() but the age and weight will not be a floating point values so parseInt() make more sense here.

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

Based on your plunkr array:

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);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • `parseInt(str, 10)`. [Why do we need to use radix?](https://stackoverflow.com/q/6611824/2025923) – Tushar Jun 06 '18 at 07:29
0

You can loop through your array and convert string to number:

var arr = [{ age : "1", weight : "1"}, { age : "2", weight : "2"}  ];
arr.forEach(e =>  { 
  e.age = +e.age;
  e.weight = +e.weight;
});
console.log(arr);
Faly
  • 13,291
  • 2
  • 19
  • 37
0

You can create a modified object using Object.keys() and reduce():

let obj = { age : "1", weight : "1"};

let result = Object.keys(obj).reduce((a, c) => (a[c] = Number(obj[c]), a), {});

console.log(result);
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
0

Simply use parseInt()

Replace the following in your add function

$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>
Ahsan Ali
  • 4,951
  • 2
  • 17
  • 27
0

Change the inputs to 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>

OR convert to number in add function:

$scope.add = function(params , age, weight) {        
    $scope.params = params;      
    age && $scope.params.age = +age;
    weight && $scope.params.weight = +weight;
};
georgeawg
  • 48,608
  • 13
  • 72
  • 95