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!!!

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

parseInt(str, 10). Why do we need to use radix?
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);

Comments

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

Comments

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>

Comments

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;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.