Here is my component code. In this code I have stored all data in a local array to find an item from this array. But when I try to get an element from this array it shows undefined.
   //-------------------------------------------------------------           
    Component.ts
            export class AccountsComponent implements OnInit
            {
                retVal = [];
                constructor(
                public service:AccountingService) 
                { 
                    this.service.getAccounts().forEach(item=>{
                    this.retVal.push(item['chartofaccount']); // Locally stored the value to an array//
                    });      
                }
                ngOnInit() 
                {
                    console.log(this.getAccountById(2));
                }
                getAccountById(id)
                {
                    return this.retVal.find(x => x.id === id);  // Return value showed undefined//
                }
            }    //-------------------------------------------------------------   
            Service.ts
             getAccounts():Observable<ChartOfAccount[]>
                {
                    return this._htc.get<ChartOfAccount[]>(this.apiUrl+'chart-of-account', httpOptions)
                    .pipe(
                        tap(data => console.log("Data:", data)),   
                        ); 
                }
 
     
    