Remus Remus - 28 days ago 31
TypeScript Question

Angular2 filter array of objects

i will start by adding the code, result i get and in the end what i would like to obtain and if it is possible.

The result which i'm getting is Array[object, object, ...] where object is Array

export class SomeService {
....
....
public someFunction(): MyObject[]{
Observable
.forkJoin(this.userItemsA(userId), this.userItemsB(userId), etc)
.filter(each => {
for (let array of each) {
let x: any = <any> array;
return x.length > 0;
}
})
.map(result => {
return result;
})
.subscribe(result => {
/// what i would like to do for example assuming only 1st array has items
/// do something here with result[0]
/// return MyObject[] from result[0]
});
....
}
}



Filter structure


filter structure

I'm in my early learning stage of angular2 and reactive programming, what i would like is to filter so that the map result will be only the arrays that have at least 1 item.

Thank you

Answer

Instead of .filter use .map

.map(each => {
    return each.filter(array => array.length > 0)
}
Comments