I created a filterList function to compare a mainList with a subList1. The function's goal is to identify the elements in the main list that are not present in subList1 and store them in subList2.
public filterList(mainlist: Selectitem[], subList1: Selectitem[]) {
let mainlistCopy = mainlist;
let subList2: SelectItem[] = [];
if (subList1) {
mainlistCopy.forEach((element) => {
if (element.title) {
let itemExists = subList1.find((x) => x.id ==element.id);
if (!itemExists) {
subList2.push(element);
}
}
});
}
return subList2;
}
Both lists are based on the following model:
interface IList {
id?: string;
// ...
}
When I run the function, I get an empty subList2 even though subList1 is a subset of mainList. Can anyone assist me in resolving this issue?