Задать вопрос
@fessss

Как правильно удалить из массива объект по ключу?

У меня есть метод который создает строит treeview
addChild(item) {
      this.companyShow = true;
      this.selectedItem = item;
    },
addCompany() {
      if (!this.selectedItem.children) {
        this.$set(this.selectedItem, 'children', []);
      }

      const name = this.company;
      // eslint-disable-next-line no-plusplus
      const id = this.nextId++;
      this.selectedItem.children.push({
        id,
        name,
      });
      this.company = '';
      this.companyShow = false;
      this.$refs.tree.updateAll(true);
    },


Но вот как сделать удаление. Совсем не могу понять.
Попробовал так, но не выходит
removeChild(items, i) {
      return items.map((item) => ({ ...item })).filter((item) => {
        if ('children' in item) {
          item.children = this.removeChild(item.children, i.id);
        }
        return item.id !== i.id;
      });
    },
  • Вопрос задан
  • 133 просмотра
Подписаться 1 Средний Комментировать
Ответ пользователя Swert К ответам на вопрос (2)
swert-tech
@swert-tech
Зачем писать дополнительные функции если у vue есть splice()
Ответ написан
Комментировать