简体   繁体   中英

Typescript Array.filter empty return

Problem statement

I've got problem with an object array I would like to get a sub object array from based on a object property. But via the Array.filter(lambda{}) all I get is an empty list.

The object is like:

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

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

The method to get the subArray is:

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

   return list;
}

Analys

Namely two things been done ensure filter comparing works and that the data is "as expected".

Brekepoints in visual studio code

Via break points in the return and filter compareison I've inspected that the property value exists (by conditions on the break point) and that the "list" which is returned is empty.

I would like to point out that I use a Typescript linter which usally gives warning for the wrong types and undefined variable calls and such so I am quite sure it shouldn't be an syntax problem.

Tested via javascript if it works in chrome console

在此处输入图像描述

remove braces inside callback function

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

   return list;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM