1

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

3
  • Can you please create a minimal reproducible example, preferably using a code snippet Commented Jan 8, 2021 at 3:12
  • hi @NickParsons plz check now Commented Jan 8, 2021 at 3:19
  • 1
    Array.filter is the way to do this. But you are also modifying the very list you are iterating over, which is not recommended. Try to use the functional-style methods of Array for these types of problems and all of these issues usually go away. Commented Jan 8, 2021 at 7:57

1 Answer 1

4

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

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

Comments

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.