使用多滤镜过滤对象数组

时间:2019-09-30 18:42:41

标签: javascript

我正在尝试使用“过滤器”对象过滤对象数组。下面的“过滤器”对象应过滤类型为“玩具”或“电子产品”,语言:“英语”或“西班牙语”的对象的数组。我正在尝试创建一个函数,该函数将基于提供的过滤器作为示例返回最后一个对象(name: 'StoreF')。

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"]
            },
        ]

使用此功能可以正常工作,直到为同一个类别(即language: ["English", "Spanish"])引入2个过滤器为止。

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);

我在做什么错了?

1 个答案:

答案 0 :(得分:3)

对于OR,您需要some而不是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>

还要注意商店中的多种错字,Engilsh不会为英语返回true