Currently, I am utilizing Angular2 with Typescript and making use of the filter method.
The functionality of the filter() method involves creating a new array that contains elements which successfully pass the test defined by the given function.
However, an issue arises when employing the logical operator "or" as it only yields either 1 or 0 result...
Here is how the code looks:
public filter(url: string, id: number): Observable<any> {
return this.http.get<any>(url).pipe(map(param => {
console.log('date' , param[1]['params'].filter(dato => new Date(dato.startdatevalidity).toLocaleDateString() === '15/3/2016'));
let filter = param[1]['params'].filter(dato => dato.id === id)
|| param[1]['params'].filter(dato => new Date(dato.startdatevalidity).toLocaleDateString() === '15/3/2016')
|| param[1]['params'].filter(dato => new Date(dato.enddatevalidity).toLocaleDateString() === '15/4/2016');
console.log('filter' , filter );
return filter;
}
));
}
Upon examining the console log output from (date), an Array containing 10 items is displayed - which is satisfactory.
Nevertheless, two dilemmas arise when dealing with the variable 'filter':
1. The first problem entails that when sending an id, the filter process solely incorporates that particular id resulting in just 1 outcome.
2. The second issue emerges when failing to send any id, leading to no results being generated.
In both these scenarios, the expected outcome should ideally be equivalent to the array length 10 displayed in the initial console log (date).