首页 > 解决方案 > 无法使用角度 4 中的打字稿过滤嵌套对象元素

问题描述

我正在尝试使用打字稿从嵌套对象中删除一个元素。我尝试过的选项似乎没有过滤,其他选项给了我语法错误。有人可以告诉我问题可能是什么

this.results is of the type   public results: Array<NpvAnalysisResult> = [];

Npv分析结果

export interface NpvAnalysisResult extends NpvAnalysis {
    captiveNetCost: number;
    commNetCost: number;
    selfNetCost: number;
    captiveInsPremiumPaidTotal: number;
    captiveInsTaxDeductionTotal: number;
    captiveInsLoanToParentTotal: number;
    captiveInsCapitalContributionTotal: number;
    captiveDividentDistributionTotal: number;
    captiveInsTerminalValueTotal: number;
    commInsPremiumPaidTotal: number;
    commInsTaxDeductionTotal: number;
    selfInsDiscountedLossesPaidTotal: number;
    selfInsDiscountedTaxDeductionTotal: number;
    boxplotChartSeries: any[];
}

Npv结果

NpvAnalysis extends NpvResults

export interface NpvAnalysis extends NpvResults {
    strategyName: string;
}

导出接口 NpvResults {

    commInsYear: number[];
    commInsPremiumPaid: number[];
    commInsTaxDeduction: number[];
    commInsDiscountedTaxDeduction: number[];
    commInsDiscountedLossesPaid: number[];
    commInsGraphData: number[];
    selfInsYear: number[];
    selfInsDiscountedLossesPaid: number[];
    selfInsDiscountedTaxDeduction: number[];
    selfInsGraphData: number[];
    captiveInsYear: number[];
    captiveInsPremiumPaid: number[];
    captiveInsTaxDeduction: number[];
    captiveInsLoanToParent: number[];
    captiveInsCapitalContribution: number[];
    captiveDividentDistribution: number[];
    captiveInsTerminalValue: number[];
    captiveInsGraphData: number[];
    chartSeries: SeriesGeneric<BoxPlotSeriesData>;
}

BoxPlotSeries数据接口

export interface BoxPlotSeriesData  {

    low: number;
    q1: number;
    median: number;
    q3: number;
    high: number;
    color: string;
    name: string;
}

class SeriesGeneric<T> { 

    public data: T[];
}

调试时的代码

[![在此处输入图像描述][1]][1]

我正在尝试使用下面的代码进行过滤,但似乎没有过滤。

this.results.filter(x=> x.chartSeries.data.find(x=> x.name === 'Commercial Option'));

属性的输出

Array(3) [Object, Object, Object]
length:3
__proto__:Array(0) [, …]
0:Object {commInsYear: Array(15), commInsPremiumPaid: Array(15), commInsTaxDeduction: Array(15), …}
captiveDividentDistribution:Array(16) [0, 0, 0, …]
captiveDividentDistributionTotal:0
captiveInsCapitalContribution:Array(16) [200000, 0, 0, …]
captiveInsCapitalContributionTotal:200000
captiveInsGraphData:Array(5) [1388107.00397383, 1391311.48360946, 1392127.30966767, …]
captiveInsLoanToParent:Array(16) [-111.240737236554, -108.323486790662, -105.436616545005, …]
captiveInsLoanToParentTotal:-1282.248023850004
captiveInsPremiumPaid:Array(16) [131898.108682546, 132963.002657693, 134038.336769846, …]
captiveInsPremiumPaidTotal:2095178.5368629089
captiveInsTaxDeduction:Array(16) [-27698.6028233354, -27922.2305581151, -28148.0507216675, …]
captiveInsTaxDeductionTotal:-439987.4927412096
captiveInsTerminalValue:Array(16) [0, 0, 0, …]
captiveInsTerminalValueTotal:-461755
captiveInsYear:Array(16) [1, 2, 3, …]
captiveNetCost:1392153.7960978495
chartSeries:Object {data: Array(3)}
    data:Array(3) [Object, Object, Object]
    length:3
    __proto__:Array(0) [, …]
    0:Object {low: 1388107.00397383, q1: 1391311.48360946, median: 1392127.30966767, …}
    1:Object {low: 0, q1: 0, median: 0, …}
    color:"#C111A0"
    high:0
    low:0
    median:0
    name:"Commercial Option"
    q1:0
    q3:0
    __proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}
    2:Object {low: 21503.9638877926, q1: 24305.1060722588, median: 25003.3902831156, …}
  [1]: https://i.stack.img

ur.com/GaIoZ.png

标签: angulartypescript

解决方案


我终于找到了答案

get chartSeries() : Array<NpvAnalysisResult> {
    if(!this.CommercialPremium) {

      return this.results.map(x=> {
        const i = x.chartSeries.data.findIndex(y => y.name.toUpperCase() === 'COMMERCIAL OPTION');
        if (i > -1) {
          x.chartSeries.data.splice(i, 1);
        }
        return x;
      });

推荐阅读