'Filtering numbers or strings in a comma delimited object

I am using multi-select to filter out data.

        <Multiselect
            v-model="roles"
            class="input1"
            placeholder="Select Roles"
            mode="tags"
            :searchable="true"
            :options="roleOptions"
        />

        <Multiselect
        v-model="sub_organization"
        class="input1"
        placeholder="Select sub-organization"
        mode="tags"
        :searchable="true"
        :options="suborgOptions"
        />

data:() => ({
    mode: "tags",
    closeOnSelect: false,
    roleOptions: [],
    suborgOptions: [],
    searchable: true,
    sub_organization: [],
    roles: [],
    filteredData: [],
    fetchedData: [],
}),

    searchResult() {
        this.filteredData = this.fetchedData.filter((data) => {
            // var intRoles = parseInt(data.roles.split(", "))
            // var intSuborgs = parseInt(data.suborgs.split(", "))
                return (
                // intSuborgs == this.sub_organization &&
                // intRoles == this.roles
                data.suborgs.includes(this.sub_organization) &&
                data.roles.includes(this.roles)
            );
        });
    },




data.roles = {1, 3}, 
{1, 4, 5, 7}, 
{10, 14}, 
{1, 9}, 
{2, 4, 6, 8}, 
{4, 5},
{4, 10}, 
{9, 1, 4}

for example: when I use includes and I searched 1 it returns all data.roles with 1 in it including 10, 14, 4, 10 etc.

using includes():

searching 1 returns {1, 3}, {1, 4, 5, 7}, {10, 14}, {1, 9}, {4, 10}, {9, 1, 4}

searching 4 returns {1, 4, 5, 7}, {10, 14}, {4, 10}, {9, 1, 4}, {4, 5}, {2, 4, 6, 8}

as you can see I commented out intRoles and intSuborgs, I tried using parseInt and then split it, when I search 1 it returns only the objects that have 1 in index 0

using parseInt and split:

searching 1 returns {1, 3}, {1, 4, 5, 7}, {1, 9}

searching 4 returns {4, 5}, {4, 10},

What I want to happen is when I search 1 it would return only the objects that has 1 in it excluding double digits with 1, or since I am using multi-select searching 1 and 4 returns objects with 1 and 4 in it excluding double digits with 1 and 4 also.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source