One solution you could use below.
let strategypricingDetail = [
{
id: 3,
name: "test",
price: 30,
daysOfWeek: [
{ id: 1, name: "Monday" },
{ id: 2, name: "Tuesday" },
{ id: 3, name: "Wednesday" }
]
},
{
id: 23,
name: "Testing2",
price: 10,
daysOfWeek: [
{ id: 1, name: "Monday" },
{ id: 2, name: "Tuesday" }
]
},
{
id: 13,
name: "Testing3",
price: 14,
daysOfWeek: [
{ id: 1, name: "Saturaday" },
{ id: 2, name: "Sunday" }
]
}
];
let finalList = [];
// Create list of eligible days for the search
let daysToInclude = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
];
strategypricingDetail.forEach((item, index) => {
for (let i = 0; i < item.daysOfWeek.length; i++) {
// Check if the day is in the eligible list
if (daysToInclude.includes(item.daysOfWeek[i].name)) {
finalList.push(item);
break;
// When found, break out of the search after adding it to the final list.
}
}
});
console.log(finalList)
In this solution, you loop through the available items, and then compare each item's 'daysOfWeel' list with a list of eligible days.
As soon as it finds one, it'll stop searching, and add that item to a new list, until you end with the list of appropriate days.
[30,10]?