Search code examples
javascriptangularobservablesubscribe

How to I get the index of a subscribed object in Angular component file?


this.arrName.subscribe(data, (index as i) => { //fake implementation
    let myData = data.inside[i].keyName;
}); 

I want to implement something like this.


Solution

  • you can do it like this

    this.arrName.subscribe(data => {
       for(let i in data.inside){
        let myData = data.inside[i].keyName;
      }
    });
    

    but you need data as

    { 
     inside : [
                 {keyName: ...}
              ]
    }
    

    as data.inside[i] access ith index of data.inside, or if you have data as array use data[i]