3

I've got a very basic array:

specialityTest: [{person:'nurse', text:'text1'}, 
{person:'nurse', text:'text1'}, 
{person:'physician', text:'text1'}, 
{person:'physician', text:'text1'}]

I'm trying to filter this using basic filter function:

      this.specialityTest.filter((person) => {
        return (person =="physician")
      })

and then copy filtered array:

  methods: {
   seedSpeciality(){
      this.nurseListSpeciality2 = JSON.parse(JSON.stringify(this.specialityTest.filter(
      (person) => {
        return (person =="physician")
      })))
  },
 }

and this is not working for me... I've got error:

Uncaught (in promise) TypeError: this.specialityTest.filter is not a function at VueComponent.seedSpeciality (AddOfferDialog.vue?f4fc:1771) at VueComponent.boundFn [as seedSpeciality].

What am I doing wrong here?

edit: I run this function from api call:

  fetchSpecialityArray(){
    this.$http.get('http://127.0.0.1:8000/api/speciality')
      .then(response => response.json())
      .then(result => this.specialityTest = result).then(this.seedSpeciality.bind(this))
  },
5
  • Do a console.log(this.specialityTest) to see if it's what you expect (may be undefined). Also, your filter condition should be person.person === "physician" Commented Apr 12, 2018 at 18:13
  • console.log(this.specialityTest) gives the correct array of 90 elements. changing filter conditions to person.person is not changing the error Commented Apr 12, 2018 at 18:16
  • 1
    Your error seems to indicate that you're calling the method in a Promise. Is this the case? If so, this may not be correctly binding and you may need to explicitly bind the value. Alternatively, you can store this inside a variable accessible via closure and then use that variable instead. Commented Apr 12, 2018 at 18:22
  • @B.Fleming this could be the case - see my edit Commented Apr 12, 2018 at 18:26
  • @blex correct. console.log in seedSpeciality shows correct array Commented Apr 12, 2018 at 18:32

2 Answers 2

6

For me it works fine.Take a look:

<div id="app">
  <button @click="filter">Filter</button>
</div>

new Vue({
  el: "#app",
  data: {
    specialityTest: [
      {person:'nurse', text:'text1'}, 
      {person:'nurse', text:'text1'}, 
      {person:'physician', text:'text1'}, 
      {person:'physician', text:'text1'}
    ]
  },
  methods: {
    filter() {
        let filtered = this.specialityTest.filter((el) => {
        return (el.person =="physician")
      })
      console.log(filtered)
    }
  }
})

You are looping through array of object with filter function so in each loop you get the object.So if you use el.person == "physician" it will work fine

Take a look to my jsfiddle

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

10 Comments

When you use the function filter,you are looping through array of objects.so in each loop you are getting the object.So you cannot use person == "physician",but if you want person.person == "physician".Try my code it works fine
this is not solving the error - see updated question. Maybe because I call this function in a Promise could be the case
to understand the question.You want to filter the specialityTest property when you get response from backend?I am not clear where is the problem but obviously my response is right when you use filter function
no doubt that your code is correct, I've changed my code, but gets the same error. I don't know the case :/
Please give a clear description of your problem.What i am understanding is that you get from api data that will fill the specialityTest property.Then you want to filter this property.Am i right?
|
0

If you are using vuejs 3 setup api and what you are trying to filter is from an external api call, consider appendding data after .value.

e.g specialityTest.value.data.filter(...)

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.