简体   繁体   English

为什么不能使用map()函数对模型中的数组进行迭代

[英]Why Can't Iterate over an array in my model using the map() function

i have angular 7 component which is tied to a model and there is an array inside that model, the array was populated from a service. 我有一个绑定到模型的angular 7组件,并且在该模型内部有一个数组,该数组是从服务填充的。 and it's populated. 并被填充。

the problem is i can't map over the array although it has elements there. 问题是尽管其中有元素,但我无法映射到数组。 when i console it it shows the array has element. 当我控制台它显示数组具有元素。 then i tried to console typeOf(array) it always gives object although it is an array !!. 然后我试图安慰typeOf(array)它总是给对象,尽管它是一个数组!

i tried using this soluation but it didn't help either. 我尝试使用这种解决方案,但也无济于事。

any help please? 有什么帮助吗?

 export class FooModel {  
   foo : Foo  
   bars: Bar[];
 }

export class SomeComponent implements OnInit {
   model: FooModel;

   constructor(private service: ProjectService) {
    this.model = new FooModel();
    this.model.bars = [];
   } 

   ngOnInit() {
     this.service.getFoos().subscribe((result: any) => {
     // data is populated fine
     this.model=  <FooModel>result.data; 
     });

     Console.log(this.model); // the model has data at this point

     const arr = this.model.bars.map(a=> {
        // never comes here
        return a;
     });

     console.log(arr); // nothing is displayed here

    // this works why ??
    const arr2 = [1,2,3].map(s=> {
        return s;
    }
    console.log(arr2); // it displays [1,2,3]
   }
}

As the request is asynchronous, you might need to place the logic within the subscribe, 由于请求是异步的,因此您可能需要将逻辑放置在订阅中,

this.service.getFoos().subscribe((result: any) => {
     // data is populated fine
     this.model=  <FooModel>result.data; 
      const arr = this.model.bars.map(a=> {
        // never comes here
        return a;
     });
     console.log(arr);
});

subscription is asynchronous so while it is still working the next line operation in the execution stack will be performed in this case the map you have after the subscription meanwhile it is still being populated in the background. 订阅是异步的,因此在其仍在工作时,将执行执行堆栈中的下一行操作,在这种情况下,订阅后您拥有的映射将同时在后台填充。 You can try mapping in another life cycle hook say viewChecked hopefully it works. 您可以尝试在另一个生命周期挂钩中进行映射,例如说viewChecked希望它能起作用。 #cheers #干杯

Please look at the comments 请看评论

 export class FooModel {  
       foo : Foo  
       bars: Bar[];
     }

    export class SomeComponent implements OnInit {
       model: FooModel;

       constructor(private service: ProjectService) {
        this.model = new FooModel();
        this.model.bars = [];
       } 

       ngOnInit() {
         this.service.getFoos().subscribe((result: any) => {
         // data is populated fine
         this.model=  <FooModel>result.data; 
         });
        // the following starts to execute even before the model is populated above.
         const arr = this.model.bars.map(a=> {
            // never comes here because this.model.bars is empty at here and the length is 0 and nothing inside map executes
            return a;
         });

         console.log(arr); // nothing is displayed here because it has nothing inside

        // this works why ?? because you are using on an array which has some items.
        const arr2 = [1,2,3].map(s=> {
            return s;
        }
        console.log(arr2); // it displays [1,2,3]
       }
    }

So as Sajeetharan suggested, you have keep it inside subscribe() 因此,正如Sajeetharan所建议的那样,您将其保留在subscribe()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM