Working in AngularJS, I am handling an array of objects and aiming to display filtered data based on the first letter. My initial method used was as follows. HTML:
<p ng-repeat="film in filmList | startsWith:'C':'title'">{{film.title}}</p>
JS:
.filter('startsWith', () => {
return (items: any, prefix: any, itemProperty: any) => {
if (items && items.length) {
return items.filter((item: any) => {
var findIn = itemProperty ? item[itemProperty] : item;
return findIn.toString().indexOf(prefix) === 0;
});
}
};
});`
Although this approach achieved success, my current objective is to filter (always considering the first letter) based on dual options. For instance, filtering movies with titles commencing with 'B' or 'C'.
I attempted the following code adjustment, which unfortunately did not produce the desired outcome:
HTML:
<p ng-repeat="film in filmList | startsWith:'[B, C]':'title'">{{film.title}}</p>
JS:
.filter('startsWith', () => {
return (items: any, prefixes: any, itemProperty: any) => {
if (items && items.length) {
return items.filter((item: any) => {
var findIn = itemProperty ? item[itemProperty] : item;
for(let i = 0; i < prefixes.length; i++){
return findIn.toString().indexOf(prefixes[i]) === 0;
}
});
}
};
});