My goal is to efficiently filter multiple attributes with multiple values
'arr' represents a list of all products, and 'f_...' indicates the attribute such as color or type.
'namet' denotes the selected attribute by the user.
'keyt' contains the values associated with 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"] }
]
The arrays selected by the user for searching purposes
To extract the attributes, following code can be used:
var namet = ['f_106', 'f_106', 'f_105', 'f_105', 'f_104'];
var keyt = ['1000' , '1001', '993', '996', '985'];
Alternatively,
var chosenKey = ['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 am looking for a method that iterates through the data to generate results similar to 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 apply the filtering process like this:
const f = arr.filter(filtered);
An alternative approach could involve applying a different method to filter products based on multiple attributes.