首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角7:遍历复杂对象并填充表

角7:遍历复杂对象并填充表
EN

Stack Overflow用户
提问于 2019-04-08 20:24:53
回答 1查看 473关注 0票数 0

我试图把物体传递给一个角度分量。我需要循环遍历对象并填充表。目前,我已经传递了两个对象。我能够获得TrackRecord对象的长度,因为它是简单的数组。我似乎无法得到FundStatistics对象的长度,因为它似乎很复杂。我猜想对象中有数据。

FundStatistics的JSON结构

代码语言:javascript
复制
"{"237146":[{"m_Item1":"ArithmeticMean","m_Item2":0.005521221052631577,"m_Item3":0.01912607076595362},{"m_Item1":"AverageGain","m_Item2":0.038913171935483874,"m_Item3":0.13479918175184283},{"m_Item1":"AverageLoss","m_Item2":-0.03429225884615385,"m_Item3":-0.11879186925568348}]}"

循环组件并传递数据

代码语言:javascript
复制
 <div class="panel panel-default">
        <div class="panel-heading product-heading">
            <span style="font-size: 14px; font-weight: bold;">Performance Track Record and Statistics</span>
        </div>
        <div *ngIf ="ViewModel.FundPerformance" class="panel-body" style="width:100%">
            <div *ngFor="let ftr of ViewModel.FundPerformance">
                <div style="clear: both;"></div>
                <br /><br />
                <fund-statistics [fundStatistics]="ftr.FundStatistics"  [fundTrackRecord]= "ftr.TrackRecord"></fund-statistics>
            </div>

        </div>
    </div>

组件代码

代码语言:javascript
复制
export class FundStatisticsComponent implements OnInit {
    @Input() fundStatistics: any;
    @Input() fundTrackRecord: any;

    ngOnInit() {
    }
}

下面是我想要填充的表格。我基本上想要填充数组的m_Item2,例如表中第一列的ArithmeticMean和表中算术平均数的第二列下的ArithmeticMean的m_Item3。

代码语言:javascript
复制
<div *ngFor="let fundStats of fundStatistics">

    <table class="statsTable">
                <tr>
                    <td></td>
                    <td colspan="2" class="tableItem header">Fund Name</td>

                    <td colspan="2" class="headerTableItem header">Benchmark</td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td class="tableItem bold">Monthly </td>
                    <td class="tableItem bold">Annualized</td>
                    <td class="tableItem bold">Monthly </td>
                    <td class="tableItem bold">Annualized</td>
                </tr>
                <tr class="rowItem">
                    <td class="titleTableItem header">Compound ROR</td>
                    <td class="tableItem"></td>
                    <td class="tableItem"></td>
                    <td class="tableItem"></td>
                    <td class="tableItem"></td>
                </tr>
                <tr class="rowItem">
                    <td class="titleTableItem  header">Arithmetic Mean </td>
                    <td class="tableItem"></td>
                    <td class="tableItem"></td>
                    <td class="tableItem"></td>
                    <td class="tableItem"></td>
                </tr>
                <tr class="rowItem">
                    <td class="titleTableItem  header">Standard Deviation</td>
                    <td class="tableItem"></td>
                    <td class="tableItem"></td>
                    <td class="tableItem"></td>
                    <td class="tableItem"></td>
                </tr>

    </table>            
  </div>   
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-08 20:48:05

*ngFor不支持对象或地图上的迭代。您可以使用Object.keys(fundStatistics)获取密钥,然后将它们用作*ngFor的值。

代码语言:javascript
复制
export class FundStatisticsComponent implements OnInit {
    @Input() fundStatistics: any;
    @Input() fundTrackRecord: any;
    fundStatisticksKeys = [];

    ngOnInit() {
        this.fundStatisticksKeys = Object.keys(this.fundStatistics);
    }
}

然后在模板中执行以下操作

代码语言:javascript
复制
<div *ngFor="let key of fundStatisticksKeys">

并将当前元素作为fundStatistics[key]

从角度6.1开始,还有一种叫做KeyValuePipe https://angular.io/api/common/KeyValuePipe#description的新管道,它可以精确地用于这种情况

在这种情况下,您不需要事先获得对象键,只需在模板中使用管道,就可以在模板中获得fundStats.keyfundStats.value,比如fundStats.value.m_Item1

代码语言:javascript
复制
<div *ngFor="let fundStats of fundStatistics | keyvalue">

然后,编辑,为了填充表,您可以使用这样的插值,如果我正确理解您需要什么。value将保存正在循环的条目的值

代码语言:javascript
复制
<tr class="rowItem">
    <td class="titleTableItem  header">Arithmetic Mean </td>
    <td class="tableItem">{{ fundStats.value.m_Item2 }}</td>
    <td class="tableItem">{{ fundStats.value.m_Item3 }}</td>
    <td class="tableItem"></td>
    <td class="tableItem"></td>
</tr>

如果您没有固定数量的m_Item*值,也可以使用ngFor对这些值进行循环

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55581241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档