I am attempting to narrow down the list based on offerings
const questions = [
{ "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]},
{ "id": 1505, "offerings": [{"code": "AA"},{"code": "ABC"}]},
{ "id": 1500, "offerings": [{"code": "AC"},{"code": "DC"}]}
];
const filterByArray = ['AB', 'DC'];
My desired result is to retrieve elements that match the items in the filterByArray
[
{ "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]},
{ "id": 1500, "offerings": [{"code": "AC"},{"code": "DC"}]}
]
I have attempted filtering the array using
var filtered = questions.filter(function(element) {
return element.offerings.filter(function(cd) {
return filterByArray.indexOf(cd.code) > -1;
}).length === filterByArray.length;
});
console.log(filtered)
However, it keeps returning all elements in the array.