简体   繁体   English

迭代Map angular4内部的对象列表

[英]Iterate a List of object inside a Map angular4

export class Person{
name:string,
age:number,
listAdress:Map<string,Adresses[]>

}

I need to parse all adresses of each person 我需要解析每个人的所有地址

    <div *ngFor="let person of persons">
<div *ngFor="let adr of person.listAdress">==> it is possible ??
</div>

how can I iterate all adresses with key of Map 如何使用Map键迭代所有地址

if we suppose that the key of Map is 'Fes' 如果我们假设Map的键是'Fes'

The Map API exposes a values() method which you can use to get the Address[] . Map API公开了values()方法,可用于获取Address[]

The data structure is a bit confusing, but I'll show you how you can get the first array of Address from the person and iterate over that with *ngFor . 数据结构有点混乱,但我将向您展示如何从人员获取第一个Address数组并使用*ngFor进行迭代。 ( *ngFor requires an Array-like structure, not just any iterable). *ngFor需要类似Array的结构,而不仅仅是任何可迭代的结构)。

Create a method in your component class to convert the values iterable into an array: 在组件类中创建一个方法,将可迭代的values转换为数组:

In component class: 在组件类中:

getAddresses(person: Person): Address[] {
  return Array.from(person.listAddress.values())[0];
}

In template: 在模板中:

<div *ngFor="let person of persons">
  <div *ngFor="let adr of getAddresses(person)"></div>
</div>

You can also use the forEach() method to iterate over each value and add it to another array (again in a separate method): 您还可以使用forEach()方法遍历每个值,然后将其添加到另一个数组中(再次在单独的方法中):

In component class: 在组件类中:

getAllAddresses(person: Person): Address[] {
  const allAddresses: Address[] = [];
  person.listAddress.forEach((value: Address[]) => {
    allAddresses.push(...value);
  });
  return allAddresses;
}

In template: 在模板中:

<ul> All Addresses: 
  <li *ngFor="let adr of getAllAddresses(person)">
    {{ adr.num + ' ' + adr.street }}
  </li>
</ul>

Here is a link to a working app with both examples: https://stackblitz.com/edit/address-map 这是包含两个示例的可运行应用程序的链接: https : //stackblitz.com/edit/address-map

You cannot loop on a Map with ngFOr as it's not considered as Iterable . 您无法使用ngFOrMap上循环,因为它不被视为Iterable

To do that, you might first apply a transformation thanks to Array.from or manually looping on them as pointed out by @vincecampanale BUT it'll hurt your your performances as it'll be triggered and run on every change detection. 为此,您可以首先使用Array.from进行转换,或者如@vincecampanale指出的那样手动对其进行循环,但会触发并在每次更改检测时运行,这会损害您的性能。

If it's a small app, fine. 如果是小型应用程序,那很好。 If you care about perfs, you should rather use a (pure!) pipe to apply the transformation to the Map + trackBy on the ngFor . 如果您关心perfs,则应该使用(pure!)管道将转换应用于trackBy上的Map + ngFor

For the pipe, no need to do it yourself, you can use ngx-pipes and do 对于管道,无需自己做,您可以使用ngx-pipes并执行

<div *ngFor="let person of persons">
  <div *ngFor="let adr of person.listAdress | values"></div>
</div>

It be better for perfs and avoid coding something that does not exists. 对于perfs更好,并且避免编码不存在的内容。 Also add a trackBy and it'll be excellent :) 还添加一个trackBy,它会很棒:)

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

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