skip to Main Content

Each user in our database can have specific skills. We have a list of available skills in our db and each has it’s own unique id. We need to be able to filter users to see which ones have the skill we are looking for. For example, show all Janitors.

We’re using vuejs and this filter is a computed property.

Here’s what I have so far:

filteredResults() {
  return this.user.filter(user => this.skills.find(skill => user.skills.id == skills.id));
}

I think this is my problem "user.skills.id". I want to say: user.skills[*].id, but that’s not valid syntax.

How can I find my users where one of their skills has an id that matches my selected (in a search dropdown) skill.id?

Our data looks like this:

Users: [
  user1: {
    name: "Tom",
    id: "12345",
    skills: [
      0: {
        title: "Programmer",
        id: "12345678"
        },
      1: {
        title: "Janitor",
        id: "6788012"
       }
    ]
  },
  user2 {
    name: "Bill",
    id: "u0wb0fu3b",
    skills: [
      0: {
        title: "Landscaper",
        id: "234525"
      },
      1: {
        title: "Janitor",
        id: "6788012"
      }
    ]
  }
]
SearchedSkills: [
  0: {
      title: "Landscaper",
      id: "234525"
  },
]

2

Answers


  1. You can use Array#some.

    return this.user.filter(user => this.skills.some(
            skill => user.skills.some(({id}) => id === skill.id)));
    
    Login or Signup to reply.
  2.     const expected_id = SearchedSkills[0].id
        let filtered_users = []
    
        for (const user of Users) {
            if (user.id === expected_id)
                filtered_users.push(user)
        }
        console.log(filtered_users)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search