0

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?

1 Answer 1

2

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

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

5 Comments

you can use also the +, like {{+team.placement - team.seed}}
parseInt(team.placement) works, but I'd like to keep the string value as well, as I use the full string elsewhere in my code.
I was hoping there is a way that I can call a function that runs for every <td> that is created. Something like <td value=getPlacementInt(team.placement)>{{value - team.seed}}</td>
How about create a custom Pipe? see https://angular.io/guide/pipes
Or, in your component, declare a member parseInt = window.parseInt. Then you can just use {{ parseInt(team.placement) - team.seed }} in your template

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.