0

I have a simple array of strings, and I'm calling a function with dropdown actions that will send a string, and if it matches one in the array, I want to remove it from the array but I'm unsure how

I've seen how I could do this with splice, but I don't have IDs on this array, it's only strings. So I literally need to match the string and remove it based on that string match

How can I simply match and remove a string from the array?

here is my code:

new Vue({
      el: "#app",
      props: { 
      },
      data: {
        tierArray:['one', 'two', 'three'],
      },
      methods: {
        removeTierSelection(value){
          console.log('this is what we are removing');
          console.log(value.name); //this prints just the string such as "one" or "two", but without the quotes
        },
      }
    })

2 Answers 2

2

You can achieve it by doing this:

 const removeIndex = this.tierArray.findIndex(tier => tier === value.name)
 if (removeIndex === -1) return // means item not found

 this.tierArray.splice(removeIndex, 1)
Sign up to request clarification or add additional context in comments.

Comments

0

Another simple approach would be

this.tierArray = [...this.tierArray.filter(tier => tier !== value.name)];

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.