Angular 2 - 如何比较JSON和ngModel对象得到它们之间的区别?

时间:2017-01-17 04:22:27

标签: angular

  <select multiple [(ngModel)]="budgetSelectedVal "> 
       <option *ngFor="let budgetyear of budgetyears " value = {{budgetyear.value}} > 
                    {{budgetyear.value}} 
       </option>  
  </select>

以上代码用于双向绑定预算年度选择的下拉列表,

budgetyears

[{
        "year": "budgetYear",
        "value": "2018"
    }, {
        "year": "budgetYear",
        "value": "2017"
    }, {
        "year": "budgetYear",
        "value": "2016"
    }, {
        "year": "budgetYear",
        "value": "2015"
    }, {
        "year": "budgetYear",
        "value": "2014"
    }, {
        "year": "budgetYear",
        "value": "2013"
    }, {
        "year": "budgetYear",
        "value": "2012"
    }
]

现在我想比较预算年度的选定值(来自下拉列表)与来自服务器的JSON数据返回。例如,对于预算年度选定值(2018,2017,2016)我只收到年度(2018,2017)而不是2016年的回复然后我应该能够使用JSON响应仅显示2018年和2017年的数据,而2016年我需要设置一些默认值。

JSON响应应该如下所示:

[{
    "budgetYear": "2018",
    "amount": 424.0,
}, {
    "budgetYear": "2017",
    "amount": 1008.0,
}]

1 个答案:

答案 0 :(得分:0)

您可以使用竖线或使用 .filter 在打字稿中执行此操作,如下所示,

constructor(){ 
       this.budgetyears=this.budgetyears
                        .filter(years=>years.value==this.budgetSelectedVal)
}