What is the best way to filter an inner array of objects by key value when given an array with objects that contain options?
Consider the scenario below:
let test = [{
"options": [{
"label": "Audi",
"value": 10
},
{
"label": "BMW",
"value": 18
},
{
"label": "Mercedes Benz",
"value": 116
},
{
"label": "VW",
"value": 184
}
],
"label": "test1"
},
{
"options": [{
"label": "Adler",
"value": 3664
},
{
"label": "Alfa Romeo",
"value": 3
},
{
"label": "Alpine",
"value": 4
}
],
"label": "test2"
}
]
If we want to extract and return the object:
{
"label": "Audi",
"value": 10
}
based on a filter for keyword Audi
,
return label.toLowerCase().includes(inputValue.toLowerCase());
the code snippet I attempted used a map function like so:
test.map((k) => {
res = k.options.filter((j) => {
inputValue.toLowerCase();
if (j.label.toLowerCase().includes(inputValue.toLowerCase())) {
return j;
}
});
});