在Angular 6中从对象移除值

时间:2018-12-05 07:09:37

标签: javascript arrays angular typescript

我在Angular中有这样的物体

documents = [
{name: "sto.jpg", selected: false}
{name: "stosecen.jpg", selected: false}
{name: "red.png", selected: false}
{name: "maxresdefault.jpg", selected: false}
];

还有一些这样的数组

documentsForDelete = ["sto.jpg", "stosecen.jpg"];

我需要从documentsForDelete中删除文档中的值

这是我尝试过的,没有运气

  remove(array, element) {
    const index = array.indexOf(element);
    array.splice(index, 1);
  }

remove(documents.name, documentsForDelete);

1 个答案:

答案 0 :(得分:6)

您可以使用.filter()获得结果数组:

let data = [
  {name: "sto.jpg", selected: false},
  {name: "stosecen.jpg", selected: false},
  {name: "red.png", selected: false},
  {name: "maxresdefault.jpg", selected: false}
];

let array = ["sto.jpg", "stosecen.jpg"];

let result = data.filter(({ name }) => !array.includes(name));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }