打字稿Array.filter空返回

时间:2020-05-27 11:41:41

标签: node.js angular typescript

问题陈述

我遇到了对象数组问题,我想从一个基于对象属性的子对象数组。但是,通过Array.filter(lambda {}),我得到的只是一个空列表。

对象类似于:

export interface objectType1 {
   someName: number;
   someOtherName: string;
}

export interface ObjectType2 {
   name: string;
   other: string;
   ObjectType1: [];
}

获取subArray的方法是:

private getSubArray(toDivied: ObjectType2[], propertyValue: string){
   let list: ObjectType2[] = toDivied.filter((row:ObjectType2) => {
     row.name === propertyValue
   });

   return list;
}

分析

完成了两件事,确保过滤器比较工作正常,并且数据“符合预期”。

Visual Studio代码中的断点

通过返回和过滤器比较中的断点,我检查了属性值是否存在(根据断点的条件),并且返回的“列表”为空。

我想指出的是,我使用的是Typescript linter,它通常会为错误的类型和未定义的变量调用提供警告,因此,我非常确定这不应该是语法问题。

通过javascript测试是否可以在chrome控制台中使用

enter image description here

1 个答案:

答案 0 :(得分:1)

删除回调函数中的括号

private getSubArray(toDivied: ObjectType2[], propertyValue: string){
   let list: ObjectType2[] = toDivied.filter((row:ObjectType2) => 
     row.name === propertyValue
   );

   return list;
}