无法使用角度4中的typescript过滤嵌套对象元素

时间:2018-06-15 10:01:03

标签: angular typescript

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

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

NpvAnalysisResult

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[];
}

NpvResults

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>;
}

BoxPlotSeriesData接口

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

2 个答案:

答案 0 :(得分:0)

您应该将代码发布为代码,而不是图像。

你需要将filter的结果赋予这样的变量:

results = [{
  id: 0,
  children: [
    {id: 0},
    {id: 1},
    {id: 2},
  ]
}, {
  id: 1,
  children: [
    {id: 0},
    {id: 1},
    {id: 2},
  ]
}, {
  id: 2,
  children: [
    {id: 3},
    {id: 4},
    {id: 5},
  ]
}];

// Find every result that has a child with ID = 1
const found = this.results.filter(result => !!result.children.find(child => child.id === 1));

// Find every result that has not a child with ID = 1
const invertFound = this.results.filter(result => !result.children.filter(child => child.id === 1).length);

console.log(found);
console.log(invertFound);

答案 1 :(得分:0)

我终于找到了答案

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;
      });