Skip to content
Advertisement

i try to delete a index from an array javascript, but its not working

here is my code

subjectData = [
     {name: 'a'},
     {name: 'b'},
     {name: 'c'},
]


eachStudent.subjectName= [
    'b', 'c'
]


this.eachStudent.subjectName.forEach((v) => {
  let index = this.subjectData.map((el) => el.name.toLowerCase()).indexOf(v.toLowerCase());
      if (index > -1) {
         this.subjectData.splice(index, 1)
      }
})

I want to remove indexs from subjectData which is exist in eachStudent.subjectName

and when i console el.name and v I found that one el is similar but always return -1

Plz help

Advertisement

Answer

It’s so much easier to do this with a one liner using filter:

subjectData = subjectData.filter((item => !subjectName.includes(item.name)));

Here’s a working stackblitz:

https://stackblitz.com/edit/js-tly2yl

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement