Q: Angular component for loop I am trying to do for loop in my component but for loop is not invoked.
Dont know why it is not looping. I am able to loop  in the html file but not in the component ts file
 export class examplecomponet implements OnInit {

   somelist:[];
   somevalue:any;

   ngOnInit() {
      for(let listvalue in  somelist){
        console.log("Checking list value",listvalue)// This is not called       
      }

}
      getSomeApi() { 
         this._auth.getApi().subscribe(data => {
            this.somevalue = data.text();      
            var v = JSON.parse(this.somevalue)
            this.somelist = v;      
         }
       );
   }
}
   checkLoop() { 
 console.log("Checking list value2",this. somelist.length)// this is 
      undefined
}

This is my json response
   {
 "Demo":{
 "Sample":"Test",
  },

 "Demo1":{
 "Sample1":"Test1",
  },

 }


A: Change your logic,
   ngOnInit() {
      this.getSomeApi();
   }

   getSomeApi() { 
      this._auth.getApi().subscribe(data => {
         this.somevalue = data.text();    
         var s = JSON.parse(this.somevalue)
         this.somelist = v; //and where this "v" come from?
         for(let listvalue in  somelist){
            console.log("Checking list value",listvalue)// This is not called
      }


A: It is because at the moment you call the loop, the data from getApi() function is not receive so the somelist value is not assigned. Try put it in side the subscribe

logListValue(){
  for( let listvalue in  this.somelist){
  console.log("Checking list value",listvalue)
  }
}

//Put it in your subscribe

getSomeApi() { 
  this._auth.getApi().subscribe(data => {
    this.somevalue = data.text();
    var s = JSON.parse(this.somevalue)
    this.somelist = v;}
    //Add it here
    this.logListValue()
   );
}

