0

How can I filter like this on vue.js?

$followid = [1,2,3,4,4,5];
foreach ($array as $element)
   if (in_array($element->id, $followid))
   endif
endforeach
5
  • You need to improve your question, I fail to see ? (question mark). Commented Oct 11, 2018 at 8:55
  • Which array exactly? Commented Oct 11, 2018 at 9:23
  • It looks like you just want to check whether display.id is in this.editlistAssesments, which I assume is an array based on indexOf. You could just replace: <div v-if="display.id | ifInArray"> with <div v-if="editlistAssesments.includes(display.id)">. Commented Oct 11, 2018 at 9:27
  • sorry variable editlistAssesments change to followid , follow id have values followid= [1,2,3,4,4,5], Commented Oct 12, 2018 at 3:01
  • display.id check in_array on followid =[1,2,3,4,4,5] Commented Oct 12, 2018 at 3:12

3 Answers 3

1

You should not use a filter for this usecase. I am not sure that you have access to 'this' in a filter. I encourage you to use a computed property that returns the array to display.

computed: {
    actifOnline: function() {
        let self = this;
        return this.online.filter((o) => {
            return self.editlistAssesments.indexOf(o) > -1
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

no need to let self = this if you are using an arrow function :)
0

You need to return bool in filter:

ifInArray: function (value) {
    return this.editlistAssesments.indexOf(value) > -1 ? true : false;
}

4 Comments

i have try like this, <span >@{{ display.id | ifInArray }}</span>
and error [Vue warn]: Error in render: "TypeError: Cannot read property 'indexOf' of undefined"
and i try <div v-if="display.id | ifInArray">
error [Vue warn]: Property or method "ifInArray" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
0

I ended up on this question trying to solve a similar problem, I think.

I used a method as my solution, I had an array of users and I needed to match the current logged in user with the managers array of users on my model:

methods: {
    isManager( task, current_user ) {
        return task.managers.find(x => x.id === current_user.id);
    }
}

Usage:

<button v-if="isManager(task, current_user)">Approve</div>

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.