Guide on clearing filters in Angular 4

I'm facing an issue where I have implemented multiple filters but resetting them is not working as expected.

showOnlyMyRequest(){
    this.requests = this.requests.filter(request => request.requestedBy === 'John Doe');
  }

showAllRequest(){
    this.requests = this.requests.filter(request => request.requestedBy === '*');
  }

In the given code snippet, it's noticed that the showAllRequest() function does not reset the previous filter applied.

Answer №1

According to information found on MDN web documentation:

The filter() function generates a fresh array containing only elements that meet the criteria set by the specified function.

After using the .filter() method on your requests array, it is recommended to store the filtered results in a separate location if you wish to display both the original and filtered requests.

Answer №2

If you choose to pass a parameter to your search function from the search text field, you can experiment with this approach. Feel free to share any better solutions if you have them so that we both can learn and grow together. Thank you in advance.

searchWithParameter(parameter1) {
    if (parameter1 === '') {
        /* Call getmethod or use the array variable obtained during the get method */
    }
    this.requests = this.requests.filter(request => request.requestedBy === '*');
}

Coded version:

filterForm(search) {
    if (search === '') {
        this.getFormSettings();
    }
    this.requests = this.requests.filter(request => request.requestedBy.indexOf(search.toLowerCase()) >= 0 === '*');
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

An error has occurred during runtime due to a TypeError: The application is unable to access properties of an undefined object, specifically 'Upload'. This error has

Greetings to the amazing Stack Overflow Community! Currently, I'm developing a Next.js application that requires me to upload videos to Vimeo. To achieve this functionality, I've implemented tus-js-client for handling the uploads. However, I&apo ...

Using NodeJS Script in conjunction with Express and setInterval

Feeling stuck and unable to find clear answers for the best solution. I have a Node.js app that serves APIs with MongoDB as the database. I created a setInterval within the request to update the data every 30 seconds. This was done to prevent the Node.js ...

What is the best way to store various types of functions within a single object key?

I have a dilemma where I am storing values and individual typed functions in an array of objects. Whenever I loop through the array, all the types of all typed functions in the array are required for any value. How can I make this more specific? Check it ...

Receiving JSON information from a web address using Javascript

I'm currently faced with a challenge in extracting JSON data from a web server. Despite the absence of errors in my code, I encounter difficulties displaying any output. Below is a snippet of the issue: <!DOCTYPE HTML> <html> <head ...

The functionality of the Jquery table sorter appears to be malfunctioning, causing it to not

Recently, I included Jquery table sorter in the header of my webpage. However, as soon as I implement the following code in my javascript: $("table").tablesorter(); All my other Jquery functionalities stop working and unfortunately, the table sorter do ...

Error in ReactJS: Attempting to access the `value` property of an undefined object

An error in ReactJS occurs when trying to access a field that is undefined: "TypeError: Cannot read property 'value' of undefined" alert(event.target.value) import React , { useState }from 'react'; import {DashboardLayout} from '. ...

Error in TypeScript in VSCode when using the React.forwardRef function in a functional component

We are developing our component library using JavaScript instead of TypeScript. In our project's jsconfig.json file, we have set checkJs: true. All components in our library are functional and not based on class components. Whenever a component needs ...

Guide to making two text boxes with SimpleDialog in jQuery Mobile

I came across the link below, but unfortunately it's not working for me. jQuery Mobile SimpleDialog with two Inputs? Is there anyone who can assist me after reviewing the code snippet provided below? <script type="text/javascript> ...

What is the best way to arrange the keys of a javascript object in descending order?

I'm dealing with a JavaScript object structured like this: data: { 474481: { date: "02/14/2017", status_id: "474481" ​​ }, 497070: { date: "02/14/2017", status_id: "497070" }, 797070: { date: "02/14/2017", status_id: " ...

Make sure to include the @ symbol before "angular" in the package.json file when setting

Can someone explain to me why the Angular 2 quickstart guide suggests using a package.json file structured like this: { "name": "angular2-quickstart", "version": "1.0.0", "scripts": { "start": "tsc && concurrently \"npm run tsc:w&bs ...

Tips for resolving the Angular Firebase v.7 problem: The search for '_delegate' in the users/xxxxx cannot be conducted using the 'in' operator

I'm working on implementing the new Angular Firebase v.7 with Angular and encountering an error message: Cannot use 'in' operator to search for '_delegate' in users/1QAvZYg6aqe0GhA13tmVAINa. While I found a similar question ( Fire ...

Encountering a Firebase error: createUser failed due to missing "password" key in the first argument in AngularJS

Recently, I started learning Angular and decided to follow an online tutorial on creating a chat application. However, I encountered an issue when trying to register with an email and password - the error message "Firebase.createUser failed: First argument ...

Clear a variable that was sent through the post method in an ajax call

Within my JavaScript file, I have the following Ajax code: // Default settings for Ajax requests $.ajaxSetup({ type: 'POST', url: path + '/relay.php' + '?curr=' + currency + "&ver=" + Ma ...

Is there a way to deactivate all dot inputs on number type input in vue.js 2?

Here is an example of my HTML code: <div id="app"> <input type="number" v-model="quantity"/> </div> This is how my Vue component looks: new Vue({ el: '#app', data: { quantity: '' }, watch: { quanti ...

Is it possible to retrieve real-time scores of the players from espncricinfo through Node.js with the help of Cheerio parser?

Currently, I am facing an issue with retrieving live scores of individual players from espncricinfo. Despite my efforts, I have not been successful in fetching this data. Although some data is being retrieved dynamically, the scores are not showing up. Th ...

Input the selected checkbox options into the designated text field

My form consists of 3 arrays of checkboxes - hummus[], mayo[], jam[]. Currently, the jQuery functions disable the remaining checkboxes once the required number is checked. Query: I am attempting to transfer the values of the checked checkboxes into text f ...

loading times of Bootstrap-select are slow when used in multiples

I am facing performance issues on my website when I try to include more than 20 select options. The jQuery execution slows down significantly and there is a stop/continue alert, taking minutes to load. Is there any way to optimize the code for faster loadi ...

Issue with updating patch version using NPM standard-version

I have integrated standard-version into my javascript project. I made sure to include the release script in my package.json: "scripts": { ... "release": "standard-version" } However, after adding a commit with the mes ...

Saving numerous files with Promises

There is a Node URL (created using Express) that enables users to download static images of addresses. The calling application sends a request to the /download URL with multiple addresses in JSON format. The download service then calls Google Maps to save ...

Even though the Spotify API JSON response may be undefined, I am still able to log it using console.log()

Trying to utilize Spotify's Web Player API in order to retrieve the 'device_id' value has been a challenge. The documentation states that the server-side API call I am supposed to make should result in a 'json payload containing device ...