0

I need a function to delete some entire object inside an array:

let selectedItem = { LinkID: 1 };

let sindecs = [
  {
    estado: { id: 2, siga: "AL", nome: "Alagoas" },
    link: [
      { LinkID: 1, Link: "link1", Active: false },
      { LinkID: 2, Link: "link 2", Active: false }
    ],
    SindecID: 3
  },
  {
    estado: { id: 19, siga: "RJ", nome: "Rio de Janeiro" },
    link: [{ LinkID: 3, Link: "rio", Active: false }],
    SindecID: 4
  }
];

function removeLinkObj(sindecs, selectedItem){

// I need to fill here with a function to remove the selectedItem that match with === sindecs.link.LinkID

}

removeLinkObj(sindecs, selectedItem)

The result should be:

let sindecs = [
  {
    estado: { id: 2, siga: "AL", nome: "Alagoas" },
    link: [
    //REMOVED LINE
      { LinkID: 2, Link: "link 2", Active: false }
    ],
    SindecID: 3
  },
  {
    estado: { id: 19, siga: "RJ", nome: "Rio de Janeiro" },
    link: [{ LinkID: 3, Link: "rio", Active: false }],
    SindecID: 4
  }
];

1 Answer 1

1
function removeLinkObj(sindecs, selectedItem){

   return sindecs.map((row) => {

        row.link = row.link.filter(item => item.LinkID !== selectedItem.LinkID);
        return row;


   });

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you broo ! I test your code, I change the LinkId to LinkID and then it's work very well ! Thank you so much !
great. happy coding.

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.