Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
72 views
in Technique[技术] by (71.8m points)

javascript - Do some basic arithmetic between a string and number in html with angular

I know there must be a simple solution to this that I can't figure out.

I have an array of objects that contain these two values:

team.seed: number,
team.placement: string

team.placement will either be a simple string like 7, indicating a team made 7th place. It can also be something like 10T, meaning they tied for 10th place.

I'm creating a table that should print out all the differences between the initial seed and the final placement:

<tr *ngFor='let team of team_array'>
    <td>{{team.placement - team.seed}}</td>
</tr>

This works fine if placement does not have the trailing T, but prints a NaN value if it does. How can I handle placement so that I can get a numerical value in each case?

question from:https://stackoverflow.com/questions/65913724/do-some-basic-arithmetic-between-a-string-and-number-in-html-with-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can parse team.placement to number first,
try
paresInt(team.placement)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...