I have to do some check/match elements with conditions. A have a list of conditions, each with a specific ID. I've put for each of my elements an array with the ids of their respective affirmative conditions.
Some elements can share a condition. The goal is to, everytime a condition is "checked" by the user, the code filter the array with all elements, showing only the ones that attend the conditions.
I've made this function. But I don't know if its the "best" way..
this.selectedConditions is already maped by ID
Any help?
private filterElements(){
let filtered: any[] = [];
originalArray.forEach( item => {
let isPossibility: boolean = true;
for (let i = 0; i < this.selectedConditions.length; i++) {
if( item.condition_matrix.indexOf( this.selectedConditions[i] ) === -1 ){
isPossibility = false;
break;
}
}
if( isPossibility ){
filtered.push(item);
}
});
this.outputArray = filtered;
if(this.outputArray.length === 0){
this.outputArray.push( { <a_filler_object> } );
}
}
Elements list example:
elements: [
{
name: 'el1',
condition_matrix: [1,2]
},
{
name: 'el2',
condition_matrix: [2,3]
},
{
name: 'el3',
condition_matrix: [2,1]
}
]
Conditions list example:
conditions: [
{
id: 1
name: 'cond1'
},
{
id: 2
name: 'cond2'
},
{
id: 3
name: 'cond3'
}
]