Ways to refine data using multiple criteria

I have a list of alarm data that I need to filter based on specific conditions. If there are multiple alarms of type "pull_Alarm" and "emergency_alarm" in the same location, I want to prioritize the "emergency_alarm".

Here is my list:
    [
        {
            alarmType:"Normal_ALARM"
            location:"Ward5"
            severity:"URGENT"
        },
        {
            alarmType:"Emergency_Alarm
            location:"Ward1"
            severity:"URGENT"
        },
        {
            alarmType:"PULL_Alarm"
            location:"Ward1"
            severity:"NORMAL"
        }
    ]
    

The desired filtered list should look like this:

[
      {
        alarmType:"Normal_ALARM"
        location:"Ward5"
        severity:"URGENT"
      },
      {
        alarmType:"Emergency_Alarm
        location:"Ward1"
        severity:"URGENT"
      }
    ]
    

If anyone has any suggestions on how to apply these filters, please let me know.

Answer №1

Implement a filter using switch cases or if-else conditions.

.filter("alarmFilter", function () {
    // Specify the desired condition here
});

Then, use this filter in your expression to apply the specified condition.

Answer №2

Create a custom filter

$scope.filter1 = function(item)
    {
        return (item.severity == 'URGENT');
    };

Implement the custom filter

<div ng-repeat="item in object| filter:filter1">{{item.alarmType}}</div>

Check it out on JSFIDDLE http://jsfiddle.net/Lvc0u55v/8866/

Answer №3

To customize the filtering process, follow the steps outlined below.

angular.module('myFilters', []).
filter('byAlarmCount', function() {
  return function(alarms, severity) {
    var items = {
      severity: severity,
      out = []
    };
    angular.forEach(alarms, function(v, k)) {
        if (this.severity[v.severity] === true) {
          this.out.push(v);
        }
    }
    return items.out;
  }
});

The DOM structure is as follows:

<ul>
    <li ng-repeat="alarm in list | byAlarmCount:severityFilter">{{alarm.location}: {{alarm.alarmType}}</li>
</ul>

Answer №4

If you are in need of performing intricate filtering tasks, it is advisable to explore third-party JavaScript libraries such as lodash (https://lodash.com/) or Underscore (http://underscorejs.org/).

These libraries offer a multitude of tools that can assist with filtering, mapping, and various other commonly used utility operations.

For instance, in the current context, you could utilize _.where / _.filter from underscorejs.

var filteredData = _.where(<YOUR DATA SOURCE>, { property: VALUE, ...});

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

How to automatically close a JavaScript popup once a form is submitted and refresh the parent page

I am facing an issue with a popup on my website. I have a separate page called math.ejs that pops up when a button is pressed on the index.ejs page. However, after clicking the submit Ok button in the math.ejs popup, I want it to close and display the calc ...

Incorporate distinct items into an array using reactjs

I'm facing an issue where clicking on a certain element multiple times adds it to my array multiple times. I need help in figuring out how to add only unique elements to the array. Can anyone offer some guidance? Thank you in advance. const handleCli ...

Angular and Firefox are flagging the response from my OAuth call as incorrect, however, Fiddler is showing a different result

Currently, I am in the process of developing a Cordova application and require OAuth authentication with my Drupal backend. My main issue lies in obtaining a request token for this purpose. Despite receiving a 200 response indicating success, when inspecti ...

Retrieve the decimal separator and other locale details from the $locale service

After reviewing the angular $locale documentation, I noticed that it only provides an id (in the form of languageId-countryId). It would be helpful to have access to more specific information such as the decimal separator character. Is there a way to retri ...

Combining two observables into one and returning it may cause Angular guards to malfunction

There are two important services in my Angular 11 project. One is the admin service, which checks if a user is an admin, and the other is a service responsible for fetching CVs to determine if a user has already created one. The main goal is to restrict ac ...

Using AngularJS $http.get method returns JSON with numeric keys, but we actually do not want any keys

Currently, I am conducting local testing on an AngularJS website. The issue I am facing involves parsing JSON data using the $http.get method from a local JSON file. When I manually define the JSON within my controller, everything works smoothly. However, ...

Tips for generating table headers in a dynamically adjusted table with AngularJS

I am having difficulty creating a table with dynamic height and width, specifically with the table header. The <td> elements are displaying properly using this ng-repeat: <tr ng-repeat="set in currFormData.volume track by $index"> <td ng ...

Having difficulties in storing the checkbox selections

Whenever I switch components and return back, the checkboxes do not persist. I want to ensure that the checked checkboxes stay checked. For more information and code samples, you can visit this CodeSandbox link: CodeSandbox Link. courses.js import React ...

Ways to modify the attribute of an element in an ImmutableList({}) nested within Immutable.Map({})

Is there a way to modify the property of an item within an ImmutableList({}) that is nested inside an Immutable.Map({})? This is my current setup: const initialState = Immutable.Map({ width: window.board.width, height: window.board.height, li ...

Troubleshooting a problem with the interactive YouTube player in Safari

I have successfully implemented a custom YouTube player on my website, allowing users to watch videos within a modal box. Below is the code for reference: JS & HTML: <script src="https://www.youtube.com/iframe_api"></script> <script typ ...

Android is now asking for location permissions instead of Bluetooth permissions, which may vary depending on the version

I am currently troubleshooting a React Native application that relies heavily on Bluetooth permissions. However, I am encountering an issue with the Android platform where the Bluetooth permissions are appearing as unavailable. I have configured the permi ...

Merging two arrays concurrently in Angular 7

When attempting to merge two arrays side by side, I followed the procedure below but encountered the following error: Cannot set Property "account" of undefined. This is the code in question: acs = [ { "account": "Cash In Hand", ...

Information gathered from checkboxes and dropdown menus

When gathering information from my form fields, I know how to capture data from input fields using this code: 'formData' : { 'timestamp' : '<?php echo $timestamp;?>', 'token' : '<?php echo md5(&a ...

Customizing Google Maps API v3: Utilizing Custom Images as symbolPath Instead of Default Symbols

Recently, I discovered the fascinating feature called Symbol Animation in Google API's documentation. All aspects of my code are functioning optimally; however, I am curious to know if it is possible to substitute an image for a symbol in the followi ...

Is there a way to implement a watch on $validator.errors in Vue.js using the Vee Validation plugin?

My intention was to implement a watch on $validator.errors, so that any error that arises gets logged, To achieve this, I checked the length of errors and stored self.errors.all() in a variable, However, I'm curious if it's possible to directly ...

Selecting multiple items from a grid using the Ionic framework

I am looking to create a grid of category images in Ionic framework, where users can select multiple categories and send their values to the controller. However, I'm unsure about how to make this happen with Ionic framework. Below is the view I curre ...

Revamp your AngularJS controller for a fresh new look

How can a controller be refreshed in Angular? Take into account the scenario below: <div ng-controller="MsgCtrl" ng-repeat="m in messages">{{m}}<br></div> <script> var ws = new WebSocket(something, something); function MsgCtrl($sco ...

Looking for a JavaScript library to display 3D models

I am looking for a JavaScript library that can create 3D geometric shapes and display them within a div. Ideally, I would like the ability to export the shapes as jpg files or similar. Take a look at this example of a 3D cube: 3d cube ...

Preserving CSS styling while loading an HTML page with InnerHTML

By using the following code snippet, I have successfully implemented a feature on my website that allows me to load an HTML page into a specific div container when clicking a link in the menu. However, I encountered an issue where the background color of ...

Steps to have index.html display when running the build command in a Svelte project:

Greetings everyone, I'm brand new to using Svelte and have a question that's been on my mind. I recently developed a small app in Svelte that works perfectly fine during development. However, when I run npm run build for production, the output ...