I am currently working on implementing a buy it now only filter button for listings that allow that option. However, I am facing an issue where the app crashes when the button is clicked due to a for loop in my code. Strangely, if I remove the for loop, the app does not crash. I have tried to identify the problem but I cannot seem to understand why it is crashing even though I believe the loop is correct. Can anyone spot what might be wrong with my code? The beforeCurrentFilter function is meant to reset the filter before the button click event. As there are 3 filters in total, the code should revert if more than one filter is selected.
filteredPosts: any[] = [];
posts: any[] = [];
beforeCurrentFilter: any[] = [];
buyItNowFilter() {
if (this.buyItNowStatus) {
this.buyItNowStatus = false;
console.log("ITS TRUE");
return;
}
if (!this.buyItNowStatus) {
console.log("its false");
this.beforeCurrentFilter = this.filteredPosts;
this.buyItNowStatus = true;
for (let i = 0; i < this.posts.length; i++) {
if (this.posts[i].auctionType !== "Not Available") {
console.log("LISTING HERE");
console.log(this.posts[i]);
this.filteredPosts.push(this.posts[i]);
}
}
this.posts = this.filteredPosts;
}
}