G
G
gallantalex2017-09-10 17:19:35
JavaScript
gallantalex, 2017-09-10 17:19:35

What is the best way to work with a list in Angular 2(4)?

There is a small application in angular 4. There is a service for working with a specific entity. It stores an array of objects and allows you to add / change / remove elements.
Here is an example of deletion:

deleteItem(itemId) {
    this.items.forEach((item, index) => {
      if (itemId === item.id) {
        this.items.splice(index, 1);
      }
    });
    this.localStorageService.cacheData('items', this.items);
  }

But as I was told that this option is very bad and it is better not to do it. And how, in your opinion, will the removal of elements be most efficiently and optimally implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Negwereth, 2017-09-10
@Negwereth

deleteItem(itemId) {
  this.items = this.items.filter(({id}) => itemId !== id);
  this.localStorageService.cacheData('items', this.items);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question