javascript - Removing value from object in Angular 6 - Stack Overflow

admin2025-04-19  0

I have some object in Angular like this

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

And some array like this

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

I need to remove values from documents from documentsForDelete

This is what i have tried and no luck

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

remove(documents.name, documentsForDelete);

I have some object in Angular like this

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

And some array like this

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

I need to remove values from documents from documentsForDelete

This is what i have tried and no luck

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

remove(documents.name, documentsForDelete);
Share edited Dec 5, 2018 at 7:12 Miomir Dancevic asked Dec 5, 2018 at 7:09 Miomir DancevicMiomir Dancevic 6,85216 gold badges85 silver badges154 bronze badges 2
  • 1 The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a minimal reproducible example. For more information, please see How to Ask and take the tour. – CertainPerformance Commented Dec 5, 2018 at 7:10
  • I have tried and added my solution – Miomir Dancevic Commented Dec 5, 2018 at 7:12
Add a ment  | 

1 Answer 1

Reset to default 7

You can get the resultant array using .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; }

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745058055a282524.html

最新回复(0)