*ngFor 持续同时访问两个具有相同名称的不同数组。我只想迭代一个数组。

huangapple go评论69阅读模式
英文:

*ngFor keeps accessing 2 seperate arrays with the same name concurrently. I only want to Iterate through one

问题

<mat-tab *ngFor="let data of allData" label="{{data.event.useful[0].name}}">
  <!-- The part below is fixed -->
  <div class="content">
    <mat-list role="list">
      <mat-list-item role="listitem">{{data.section[0].street_group[0].name}}</mat-list-item>
    </mat-list>
  </div>
</mat-tab>
英文:

I am trying to Iterate through an Array. The thing is the data is nested and in an object there are 2 arrays with the same name. I would like to Iterate through only one Array. Angular Iterates through both the Arrays concurrently.

I am trying to access and print the name from the first instance of street_group array only. Angular is print the name from both instances of street_group array concurrently.

Here is the Data


{
    &quot;generated_at&quot;: &quot;-&quot;,
    &quot;schema&quot;: &quot;-&quot;,
    &quot;event&quot;: {
        &quot;id&quot;: &quot;-&quot;,
        &quot;scheduled&quot;: &quot;-&quot;,
        &quot;value&quot;: false,
        &quot;timing&quot;: {
            &quot;type&quot;: &quot;-&quot;,
        },
        &quot;season&quot;: {
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;start_date&quot;: &quot;-&quot;,
            &quot;end_date&quot;: &quot;-&quot;,
            &quot;year&quot;: &quot;-&quot;,
        },
        &quot;cycle&quot;: {
            &quot;id&quot;: &quot;&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;car&quot;: {
                &quot;id&quot;: &quot;-&quot;,
                &quot;name&quot;: &quot;-&quot;
            },
            &quot;category&quot;: {
                &quot;id&quot;: &quot;-&quot;,
                &quot;name&quot;: &quot;-&quot;
            },
            &quot;type&quot;: &quot;-&quot;,
            &quot;value&quot;: &quot;-&quot;
        },
        &quot;useful&quot;: [{
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;value&quot;: &quot;-&quot;,
            &quot;value2&quot;: &quot;-&quot;,
            &quot;value3&quot;: &quot;-&quot;,
            &quot;value4&quot;: &quot;-&quot;
        }, {
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;value1&quot;: &quot;-&quot;,
            &quot;value2&quot;: &quot;-&quot;,
            &quot;value3&quot;: &quot;-&quot;,
            &quot;value4&quot;: &quot;-&quot;
        }],
        &quot;venue&quot;: {
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;city_name&quot;: &quot;-&quot;,
            &quot;country_name&quot;: &quot;-&quot;,
            &quot;map_coordinates&quot;: &quot;--&quot;,
            &quot;country_code&quot;: &quot;-&quot;,
            &quot;values&quot;: [{
                &quot;name&quot;: &quot;-&quot;
            }, {
                &quot;name&quot;: &quot;-&quot;
            }]
        }
    },
    &quot;section&quot;: [{
        &quot;locale&quot;: &quot;-&quot;,
        &quot;value&quot;: {
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;country_code&quot;: &quot;-&quot;
        },
        &quot;street_group&quot;: [{
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;type&quot;: &quot;-&quot;,
        }, {
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;type&quot;: &quot;-&quot;,
        }, {
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;type&quot;: &quot;-&quot;,
        }
        ]
    }, {
        &quot;locale&quot;: &quot;-&quot;,
        &quot;value&quot;: {
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;country_code&quot;: &quot;-&quot;
        },
        &quot;street_group&quot;: [{
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;type&quot;: &quot;-&quot;,
        }, {
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;type&quot;: &quot;-&quot;,
        }, {
            &quot;id&quot;: &quot;-&quot;,
            &quot;name&quot;: &quot;-&quot;,
            &quot;type&quot;: &quot;-&quot;,
        }
        ]
}]
}

Here is My Component


export class LiveComponent implements OnInit {
 allData: {
    generated_at: string;
    section: {
      street_group: { name: string }[];
      locale: string }[];
    event: {
      id: string;
      useful: { name: string }[];
    };
  }[] | null = null;

    constructor(private http: HttpClient ) {
    }
    ngOnInit(){
this.http.get(&#39;http://api.redacted_for_privacy&#39;).subscribe(data =&gt; {this.allData = [data as any];});
    }
    }

This is my HTML


&lt;mat-tab-group&gt;

&lt;mat-tab *ngFor=&quot;let data of allData&quot; label=&quot;{{data.event.useful[0].name}}&quot;&gt;

&gt;! The part below is broken

&lt;div class=&quot;content&quot;&gt;
  &lt;mat-list role=&quot;list&quot; *ngFor=&quot;let section of data.section&quot;&gt;
    &lt;mat-list-item role=&quot;listitem&quot; *ngFor=&quot;let street_group of section.street_group | slice:0:2&quot;&gt;{{street_group.name}}&lt;/mat-list-item&gt; 

  &lt;/mat-list&gt;
  
&lt;/div&gt;


    &lt;/mat-tab&gt;
    
  &lt;/mat-tab-group&gt; 


I have tried adding an Index number to the Section Array since it is the containing array but that breaks the code.

&lt;mat-list role=&quot;list&quot; *ngFor=&quot;let section of data.section[0]&quot;&gt;

Where of gives this error
Type '{ street_group: { name: string; }[]; }' is not assignable to type 'NgIterable<any> | null | undefined'.ngtsc(2322)

I was expecting only the first Array to render.

答案1

得分: 1

如果我理解你的问题正确,你不需要二维循环(double *ngFor),而是在第零个元素上进行单一循环。

要打印出从部分数组的第一个元素开始的所有名称:

  • 删除 mat-list 上的外部 *ngFor 指令

只循环遍历 mat-list-item 内部部分的第零个元素

你的HTML可能如下所示:

&lt;mat-list role=&quot;list&quot;&gt;
  &lt;mat-list-item
    role=&quot;listitem&quot;
     *ngFor=&quot;let street_group of data.section[0].street_group&quot;
    &gt;{{ street_grp.name }}&lt;/mat-list-item
  &gt;
&lt;/mat-list&gt;

请注意 *ngFor=&quot;let street_grp of data.section[0].street_group&quot;,只迭代 data.section[0] 中的元素;

这里还应该提到的是,有时将嵌套数据展平可能会很有用。这简化了对数据的访问和操作。

英文:

If I understand your question correctly, you don't need a two-dimensional loop (double *ngFor), but a single one on the 0th element.

To print all the names from the first element of section array:

  • Remove the outer *ngFor directive on mat-list

Only loop through the 0th element of the section inside mat-list-item

Your HTML could look like:

&lt;mat-list role=&quot;list&quot;&gt;
  &lt;mat-list-item
    role=&quot;listitem&quot;
     *ngFor=&quot;let street_group of data.section[0].street_group&quot;
    &gt;{{ street_grp.name }}&lt;/mat-list-item
  &gt;
&lt;/mat-list&gt;

notice *ngFor=&quot;let street_grp of data.section[0].street_group&quot; to iterate only the elements in data.section[0];

It should also be mentioned here that it can sometimes be useful to flatten nested data. This simplifies access and manipulation of the data.

答案2

得分: -1

没有适用于您在Angular中的目的的管道。我建议进行一个映射,将所有部分中的street_groups连接起来。

// 将在一个数组中输出所有street_groups
data.section.map((section) => section.street_group).flat()
英文:

There is no pipe for your purpose available in Angular. I suggest doing a mapping that will concatenate street_groups in all sections.

// Will output all street_groups in one array
data.section.map((section) =&gt; section.street_group).flat()

huangapple
  • 本文由 发表于 2023年5月10日 20:27:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定