3

I have numeric value as a String and I want to increment its value. In java we used to do it with Integer.parseInt but I am not getting how to achieve this in angular.

My Controller Code:

var count="18"; 
jsonData["combinationsData"]
                                         .push( {
                                            "vAxis": count,
                                         "dataValue":dataValue
                                         } )
                                        count++;//I want to increment its value

I want to increment the count value to "19","20" so on, Please suggest how could I increment a String represented numeric value.

2
  • In JS you can just use parseInt function on the string. Commented Jan 2, 2017 at 15:48
  • why are you using a string for a count in the first place? is this set somewhere else that doesn't allow for numbers? Commented Jan 2, 2017 at 15:58

3 Answers 3

4

count = "78";

count = ((+count)+1).toString();

console.log(count);

+count converts it to a number; +1 increments it; .toString() converts to it back to a string.

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

Comments

1

try this:

var count="18";  
jsonData["combinationsData"]
                                             .push( {
                                                "vAxis": count,
                                             "dataValue":dataValue
                                             } );
                                            count = (parseInt(count)+1).toString();

or:

var count=18;  
jsonData["combinationsData"]
                                             .push( {
                                                "vAxis": count.toString(),
                                             "dataValue":dataValue
                                             } );
                                            count++;

Comments

0

Use parseInt to get an int value out of your string value.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

var myInt = parseInt(count);
myInt++;

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.