I want to filter the multiple attributes with multiple values
'arr' is a list of all products and f_... is the attribute like color or type.
'namet' is the chosen attribute from the user.
'keyt' is the values of each attribute like red, yellow and green.
let arr = [
{ "id": 523, "f_105": ["992","996"], "f_104": ["985"], "f_106":["1000"] },
{ "id": 524, "f_105": ["993","996"], "f_104": ["984"], "f_106":["1001"] }
]
these arrays which user chose for searching
I can get the attrubites like this
var namet = ['f_106', 'f_106', 'f_105', 'f_105', 'f_104' ];
var keyt = ['1000' , '1001', '993', '996', '985'];
OR
var chooenKey = ['f_106', 'f_105', 'f_104']
var chosenAttr = {
"f_106": ["1000", "1001"],
"f_105": ["993", "996"],
"f_104": ["985"],
}
OR
var chosenAttr =
[
{"f_106": ["1000", "1001"]},
{"f_105": ["993", "996"]},
{"f_104": ["985"]}
]
I want a method to loop to get result like variable 'filtered'
var filtered = d =>
(d.f_106.indexOf("1000") > -1 || d.f_106.indexOf("1001") > -1) &&
(d.f_105.indexOf("993") > -1 || d.f_105.indexOf("996") > -1) &&
(d.f_104.indexOf("985") > -1)
then put it here
const f = arr.filter(filtered);
You can also give another type to filter the product with multiple attributes.
d.f_106either that way, ord["f_106"].chosenAttr(each item calledattrin this comment), and for eachattruse the object key to see which property ofdyou're checking, and see if any ofd[attrKey]values exist inattrValue, which could be done a variety of ways. You. might be over-thinking this :)