使用多个过滤器过滤对象数组

我正在尝试使用“过滤器”对象过滤一组对象。下面的“过滤器”对象应该过滤数组的对象类型:“玩具”或“电子”,语言:“英语”或“西班牙语”。我正在尝试创建一个函数,该函数将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"]

            },

        ]

使用此函数可以正常工作,直到我为同一类别引入 2 个过滤器(即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);

我究竟做错了什么?


回首忆惘然
浏览 120回答 1
1回答

慕雪6442864

的,或者需要some不every。const res = stores.filter(store =>&nbsp; &nbsp; Object.entries(filters).every(([key , value]) => value.some(e =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; store[key].includes(e)&nbsp; &nbsp; )));console.log(res);<script>const filters = {&nbsp; &nbsp; country: ["France"],&nbsp; &nbsp; type: ["toys", "electronics"],&nbsp; &nbsp; language: ["English", "Spanish"]};const stores = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: "StoreA",&nbsp; &nbsp; &nbsp; &nbsp; country: "United States",&nbsp; &nbsp; &nbsp; &nbsp; type: ["toys", "groceries"],&nbsp; &nbsp; &nbsp; &nbsp; language: ["English", "Spanish"]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: "StoreB",&nbsp; &nbsp; &nbsp; &nbsp; country: "Spain",&nbsp; &nbsp; &nbsp; &nbsp; type: ["toys"],&nbsp; &nbsp; &nbsp; &nbsp; language: ["Engilsh", "Spanish"]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: "StoreC",&nbsp; &nbsp; &nbsp; &nbsp; country: "France",&nbsp; &nbsp; &nbsp; &nbsp; type: ["autoparts"],&nbsp; &nbsp; &nbsp; &nbsp; language: ["French"]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: "StoreD",&nbsp; &nbsp; &nbsp; &nbsp; country: "Thailand",&nbsp; &nbsp; &nbsp; &nbsp; type: ["toys"],&nbsp; &nbsp; &nbsp; &nbsp; language: ["Thai"]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: "StoreE",&nbsp; &nbsp; &nbsp; &nbsp; country: "India",&nbsp; &nbsp; &nbsp; &nbsp; type: ["toys"],&nbsp; &nbsp; &nbsp; &nbsp; language: ["Engilsh"]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: "StoreF",&nbsp; &nbsp; &nbsp; &nbsp; country: "France",&nbsp; &nbsp; &nbsp; &nbsp; type: ["toys"],&nbsp; &nbsp; &nbsp; &nbsp; language: ["English", "French"]&nbsp; &nbsp; },];</script>另请注意您商店中的多个错别字,英语不会返回 true
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript