If you're looking to eliminate elements from one array based on another array, here's a solution for you.
Let me walk you through it. I've created a component that includes a function with a similar concept:
let criteriaArray = ["Tom", "Harry", "Patrick"];
let arrayToFilter = ["Miguel","Harry","Patrick","Felipe","Mario","Tom"];
let filteredResults = arrayToFilter.filter(elem => criteriaArray.indexOf(elem) < 0);
console.log(filteredResults);
The filter method does: It returns the elements of an array that satisfy the condition specified in a callback function.
And, what this callback function accomplishes is: for each element in arrayToFilter, if it is not present in criteriaArray then keep it, otherwise move on to the next element.
Here's the function implementation:
removeElements(arrayToFilter: Array<any>): Array<any> {
let results = arrayToFilter.filter(elem => this._criteriaArray.indexOf(elem) < 0);
return results;
}
this._criteriaArray is a private property initialized as:
private _criteriaArray = ["Tom","Harry","Patrick"]
Alternatively, you can handle it like this:
removeElements(arrToFilter: Array<any>, criteriaArr: Array<any>): Array<any> {
let results = arrToFilter.filter(e => criteriaArr.indexOf(e) < 0);
return results;
}
Working with two arrays makes it easier.
Enjoy implementing this! :)