I am working on an Angular application and I want to display a filtered list of data in my UI from the main component.
After working of the filter function, I want to store the filtered data in a similar manner as my original data but am not able to do so.
But I am getting,
"Cannot find a differ supporting object '[object Object]' of type 'Shubham Bhatt'. NgFor only supports binding to Iterables such as Arrays.
I want the filteredEmpData to be of the same type as empData.
I surfed and got a solution to convert the object as Arrays but is there any other way, I can do that.
P.S: Absolute noob here.
Here is what I have done so far,
export class AppComponent {
// tslint:disable-next-line:no-inferrable-types
title: string = 'Birthday App';
tDate: any = new Date();
tMonth: number = this.tDate.getMonth() + 1;
tDay: number = this.tDate.getDate();
empData: IEmpdatatype[] = [
{
'name': 'Anurag Basu',
'DOB': '1996-09-10',
'email': '[email protected]',
'imageLink': 'https://qph.fs.quoracdn.net/main-thumb-314460666-200-repbvxtqtsknkwpbemcejkxjnplkcmip.jpeg'
},
{
'name': 'Shubham Bhatt',
'DOB': '1996-06-26',
'email': '[email protected]',
'imageLink': 'https://qph.fs.quoracdn.net/main-thumb-314460666-200-repbvxtqtsknkwpbemcejkxjnplkcmip.jpeg'
}
];
filteredEmpData: IEmpdatatype = this.check();
check(): IEmpdatatype {
// console.log(typeof(this.empData));
// tslint:disable-next-line:prefer-const
let monthStr: string = this.tMonth.toString();
let dayStr: string = this.tDay.toString();
if ( monthStr.length === 1 ) {
monthStr = '0' + monthStr;
}
if ( dayStr.length === 1) {
dayStr = '0' + dayStr;
}
for (let i = 0; i < this.empData.length; i++) {
const DOBsplit: string[] = this.empData[i].DOB.split('-');
if ( DOBsplit[1] === monthStr && DOBsplit[2] === dayStr) {
this.filteredEmpData = this.empData[i];
}
}
console.log(this.filteredEmpData);
return this.filteredEmpData;
}
}
App.component.html
<div class="thisMonth">
<ul *ngFor='let filteredEmpD of filteredEmpData'>
<li><span><img [src]='filteredEmpD.imageLink' id="img-round"></span><span>{{filteredEmpD.name}}</span><span>{{filteredEmpD.DOB}}</span><span><a type="button" href="mailto:{{filteredEmpD.email}}?Subject=Happy%20Birthday">Wishes</a></span></li>
</ul>
</div>