0

I'm trying to filter an array of objects using a 'filters' object. The 'filters' object below should filter the array for object with type: 'toys' OR 'electronics', language: 'English' OR 'Spanish'. I'm trying to create a function that would return the last object (name: 'StoreF') based on the provided filter as an example.

var filters = {
                country: ["France"],
                type: ["toys", "electronics"],
                language: ["English", "Spanish"]

            }
        var stores = [{
                name: "StoreA",
                country: "United States",
                type: ["toys", "groceries"],
                language: ["English", "Spanish"]
            },
            {
                name: "StoreB",
                country: "Spain",
                type: ["toys"],
                language: ["English", "Spanish"]
            },
            {
                name: "StoreC",
                country: "France",
                type: ["autoparts"],
                language: ["French"]
            },
            {
                name: "StoreD",
                country: "Thailand",
                type: ["toys"],
                language: ["Thai"]
            },
            {
                name: "StoreE",
                country: "India",
                type: ["toys"],
                language: ["English"]
            },
            {
                name: "StoreF",
                country: "France",
                type: ["toys"],
                language: ["English", "French"]
            },
        ]

Using this function works fine until I introduce 2 filters for the same category (i.e language: ["English", "Spanish"]).

function nestedFilter(targetArray, filters) {
            var filterKeys = Object.keys(filters);
            return targetArray.filter(function(eachObj) {
                return filterKeys.every(function(eachKey) {
                    if (!filters[eachKey].length) {
                        return true;
                    }
                    if (!$.isEmptyObject(eachObj[eachKey])) {
                        return eachObj[eachKey].includes(filters[eachKey]);
                    }
                });
            });
        };

nestedFilter(stores, filters);

What am I doing wrong?

1 Answer 1

3

For OR you need some not every.

const res = stores.filter(store =>
    Object.entries(filters).every(([key , value]) => value.some(e => 
        store[key].includes(e)
    )));

console.log(res);
<script>
const filters = {
    country: ["France"],
    type: ["toys", "electronics"],
    language: ["English", "Spanish"]
};

const stores = [
    {
        name: "StoreA",
        country: "United States",
        type: ["toys", "groceries"],
        language: ["English", "Spanish"]
    },
    {
        name: "StoreB",
        country: "Spain",
        type: ["toys"],
        language: ["Engilsh", "Spanish"]
    },
    {
        name: "StoreC",
        country: "France",
        type: ["autoparts"],
        language: ["French"]
    },
    {
        name: "StoreD",
        country: "Thailand",
        type: ["toys"],
        language: ["Thai"]
    },
    {
        name: "StoreE",
        country: "India",
        type: ["toys"],
        language: ["Engilsh"]
    },
    {
        name: "StoreF",
        country: "France",
        type: ["toys"],
        language: ["English", "French"]
    },
];
</script>

Also note the multiple typos in your stores, Engilsh won't return true for English

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

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.