简体   繁体   English

如何访问 Javascript 中数组内部的元素?

[英]How do I access an element inside of an array that is inside of an array in Javascript?

Using Angular 7 and cannot find a solution to this issue.使用 Angular 7 并找不到解决此问题的方法。 My end goal is for this process:我的最终目标是这个过程:

  1. User selects their favorite kind of dessert in a drop down.用户在下拉菜单中选择他们最喜欢的甜点。
  2. Then if the user picks ice cream another drop down will populate with a list of flavors.然后,如果用户选择冰淇淋,另一个下拉菜单将填充口味列表。

Currently I have the the drop down with desserts in it and the user can select ice cream.目前我有包含甜点的下拉菜单,用户可以选择 select 冰淇淋。 I can console.log the large array which contains the array of flavors.我可以 console.log 包含口味数组的大数组。

console.log(JSON.stringify(this.desserts));

Will return:将返回:

{"DESSERT_ID":"1", "DESSERT_TYPE":"ICE CREAM", "favlorresults":[{},[{"FLAVORES":"CHOCOLATE"}]] } {"DESSERT_ID":"1", "DESSERT_TYPE":"ICE CREAM", "favlorresults":[{},[{"FLAVORES":"CHOCOLATE"}]] }

I can also console.log the array that contains the flavors:我还可以 console.log 包含风味的数组:

console.log(JSON.stringify(this.desserts[0].favlorresults[1]));

Will return:将返回:

[{"FLAVORS":"CHOCOLATE"}] [{“风味”:“巧克力”}]

But this returns as undefined:但这返回未定义:

console.log(JSON.stringify(this.desserts[0].favlorresults[1].FLAVORS));

I am expecting CHOCOLATE.我期待巧克力。

Drop down logic that isn't working:下拉不起作用的逻辑:

 <tr *ngFor="let dessert of desserts; let i = index"> <td> <select (change)="dessertChange(i)" [(ngModel)]="dessert.DESSERT_TYPE"> <option *ngFor="let desresult of desresults; let i = index" [ngValue]="desresult.DESSERT_TYPE"> {{ desresult.DESSERT_TYPE }}</option> </select> </td> <td> <select [(ngModel)]="dessert.FLAVOR"> <option *ngFor="let favlorresult of dessert.favlorresults" [ngValue]="favlorresult.FLAVOR"> {{ favlorresult.FLAVOR }}</option> </select> </td>

if this.desserts[0].favlorresults[1] is an Array ( [{"FLAVORS":"CHOCOLATE"}] ), then you need to get the element (at index 0) first.如果this.desserts[0].favlorresults[1]是一个数组( [{"FLAVORS":"CHOCOLATE"}] ),那么您需要首先获取元素(在索引 0 处)。

ie this.desserts[0].favlorresults[1][0].FLAVORSthis.desserts[0].favlorresults[1][0].FLAVORS

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

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