1

I have one object of list of products and I have created one array to apply filter function to get parameter from that array. Like below

var selectedProducts = ['music', 'dance']
var products = [
    {
        ID : 1,
        name : "pro1",
        category : "music"
    },
    {
        ID : 2,
        name : "pro2",
        category : "yoga"
    },
    {
        ID : 3,
        name : "pro3",
        category : "music"
    },
    {
        ID : 4,
        name : "pro4",
        category : "dance"
    },
]

function filterFunction(){
    return products.filter((abc) => {
        selectedProducts.forEach(function(item, index){
            return abc.category == selectedProducts[index]
        });
    });
}

What I am trying to do is when user select any checkbox, selected values will be stored in selectedProducts[] array and then on those values filter function will be called and array will be passed as paramater with forEach method. Code works fine on each iteration of loop but at the end it return me empty array of object. Is anything wrong with my code?

0

2 Answers 2

4

Returning true from your forEach() loop's callback function will not have any effect on the filter condition.

Here's an alternative approach using Array.prototype.includes():

products.filter(({category}) => selectedProducts.includes(category));

Full snippet:

const selectedProducts = ['music', 'dance']
const products = [
    {
        ID : 1,
        name : "pro1",
        category : "music"
    },
    {
        ID : 2,
        name : "pro2",
        category : "yoga"
    },
    {
        ID : 3,
        name : "pro3",
        category : "music"
    },
    {
        ID : 4,
        name : "pro4",
        category : "dance"
    },
]

function filterFunction(){
    return products.filter(({category}) => selectedProducts.includes(category));
}

console.log(filterFunction());

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

1 Comment

Also worth noting that even if it did, returning on every iteration of the loop will not result in the desired behavior.
0

Use the combination of filter and some:

function filterFunction(){
    return products.filter((abc) => {
        return selectedProducts.some(function(category){
            return abc.category === category;
        });
    });
}

some() function return true if at least one of selectedProducts matches with category of abc.

We also can use includes:

function filterFunction(){
    return products.filter((abc) => {
        return selectedProducts.includes(abc.category);
    });
}

When developing the frontend web, I prefer to use the combination of filter and some, because these functions are supported wider than includes (work with more browser versions).

Refs:

Array.prototype.filter

Array.prototype.some

Array.prototype.includes

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.