0

I have this:

  nominees: Array<{ id: number,  title: string, company: string, category: string }>;

I want to delete based on id, e.g. if the given id is 10, then I want to delete the element with the id of 10

I've been looking at splice, but honestly I'm really having a hard time. I'm quite new to Typescript

3
  • 1
    splice() is the way to go. What is your problem with it? Commented Jan 16, 2019 at 13:25
  • 2
    Can you show us the splice code that isn't working? Commented Jan 16, 2019 at 13:28
  • Well I honestly thought something like this would've worked this.nominees.splice(id);. Apparently it don't be like that. I'm looking for the similar function in java removeIf(...). Thought it was similar Commented Jan 16, 2019 at 13:53

1 Answer 1

2

If you don't want to use splice, you can use filter as below:

let result = nominees.filter(n => n.id !== 10);
Sign up to request clarification or add additional context in comments.

4 Comments

This would create a new Array, which may not be bad - but it could.
What about doing it in place ? nominees = nominees.filter(n => n.id !== 10);
Although it doesn't technically delete it, it gives me what I want. Thanks!
@Vincenzo Even then you would create a copy, which you afterwards assign to nominees . But as it seems this works for the thread opener.

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.