I have an array that looks something like this
var categorizedBooks = [
{
name: "category1",
books:[
{name:"book1", bookContainerId: 1, active: true},
{name:"book2", bookContainerId: 2, active: false},
{name:"book3", bookContainerId: 2, active: true},
]
},
{
name: "category2",
books:[
{name:"book4", bookContainerId: 1, active: false},
{name:"book5", bookContainerId: 3, active: true},
{name:"book6", bookContainerId: 4, active: true},
]
},
{
name: "category3",
books:[
{name:"book7", bookContainerId: 1, active: false},
{name:"book8", bookContainerId: 2, active: true},
{name:"book9", bookContainerId: 4, active: false},
]
},
{
name: "category4",
books:[
{name:"book10", bookContainerId: 2, active: false},
{name:"book11", bookContainerId: 2, active: false},
{name:"book12", bookContainerId: 4, active: false},
]
}
And i want to filter them on two arrays that looks something like this:
bookContainers: [1,3]
isActive: ['active', 'inactive] // an array of strings
These comes from inputs so it can change whenever. So i want to match bookContainers with their bookContainerId, and 'active'/'inactive' string array with their boolean active property and if a an object in categorizedBooks books array is empty i want to remove it.
So in this case i would get something like this (Desired output from the above):
{
name: "category1",
books:[
{name:"book1", bookContainerId: 1, active: true}
]
},
{
name: "category2",
books:[
{name:"book4", bookContainerId: 1, active: false},
{name:"book5", bookContainerId: 3, active: true}
]
},
{
name: "category3",
books:[
{name:"book7", bookContainerId: 1, active: false}
]
}
Another problem is that i don't want to modify the original array because i want to be able to call it with new arrays to filter on whenever, and get the result. Here is my GET method now:
public get bookCategories(): Array<CategorizedBook> {
let cloneToMapOf = [...this.categoriesAndBooks];
const filteredCategories = cloneToMapOf.map(categorizedbooks => {
categorizedbooks.books = categorizedbooks.properties.filter(book =>{
return (rc.includes(books.bookContainerId) &&
sa.includes(book.active ? 'active' : 'inactive'));
});
return categorizedbooks;
});
const result = filteredCategories.filter(x => x.books.length > 0 && x.books != null);
return result;
}
But for some reason this code modifies this.categoriesAndBooks so I becomes empty after time..
with the result I want to present in some HTML
<nz-collapse [nzBordered]="false" *ngFor="let categorizedBook of bookCategories;">
<nz-collapse-panel [nzHeader]="categorizedBook.name" [nzActive]="true">
<ng-container *ngFor="let book of categorizedBook.properties; trackBy:trackBy.book">
<ng-template [ngTemplateOutlet]="bookEditors" [ngTemplateOutletContext]="{book: book, form: bookValueForm}"></ng-template>
</ng-container>
</nz-collapse-panel>
</nz-collapse>
I've been sitting with this for hours.. please help me