Achieving unique values by considering two attributes

Imagine having a dataset like this:

const array = 
[
   { "CategoryDesc":"SEC Violations (Insider Trading, Securities Fraud)", "SubCatDesc":"BBC"}
   { "CategoryDesc":"SEC Violations (Insider Trading, Securities Fraud)", "SubCatDesc":"BBC"}
   { "CategoryDesc": "FRD" ,"SubCatDesc":"VRD"}
   { "CategoryDesc": "FRD" ,"SubCatDesc":"VRD"}
   { "CategoryDesc":"SEC Violations (Insider Trading, Securities Fraud)", "SubCatDesc":"BBC"}
   { "CategoryDesc":"FRD", "SubCatDesc":"BBC"}
]

We want to extract distinct values based on both CategoryDesc and SubCatDesc:

[ { "CategoryDesc":"SEC Violations (Insider Trading, Securities Fraud)", "SubCatDesc":"BBC"}
  { "CategoryDesc": "FRD" ,"SubCatDesc":"VRD"}
  { "CategoryDesc":"FRD", "SubCatDesc":"BBC"}
]

Can anyone provide guidance on how to achieve this?

Answer №1

If you want to achieve this, here is an example:

const data = 
[
   { "CategoryDesc":"SEC Violations (Insider Trading, Securities Fraud)", "SubCatDesc":"BBC"},
   { "CategoryDesc":"SEC Violations (Insider Trading, Securities Fraud)", "SubCatDesc":"BBC"},
   { "CategoryDesc": "FRD" ,"SubCatDesc":"VRD"},
   { "CategoryDesc": "FRD" ,"SubCatDesc":"VRD"},
   { "CategoryDesc":"SEC Violations (Insider Trading, Securities Fraud)", "SubCatDesc":"BBC"},
   { "CategoryDesc":"FRD", "SubCatDesc":"BBC"}
]

let finalResult=data.filter((item,index)=> data.findIndex(element=> element.CategoryDesc === item.CategoryDesc && element.SubCatDesc === item.SubCatDesc) === index)

console.log(finalResult);

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

Trigger the change event in a specialized Angular form component

After following various tutorials, I successfully implemented a custom Angular2 Component that utilizes two range inputs to create a dual range slider. Although everything is functioning properly, I am facing an issue with binding the values of the two in ...

In the world of GramJS, Connection is designed to be a class, not just another instance

When attempting to initialize a connection to Telegram using the GramJS library in my service, I encountered an error: [2024-04-19 15:10:02] (node:11888) UnhandledPromiseRejectionWarning: Error: Connection should be a class not an instance at new Teleg ...

Splitting Files into Separate Directories in Angular 7

Currently, I am in the process of setting up my Angular 7 application for production and reorganizing files into different directories. angular.json "architect": { "build": { "builde ...

combination of Electron, Node.js, and Angular

Can I integrate Angular 5 into an Electron project and have Node.js run on the server side as well? While I understand Electron allows for desktop applications, I am curious if it can also support server-side applications. Additionally, I am unsure if it ...

Concerns regarding Zod Schema Enhancements and Conditional Logic in TypeScript

I'm currently developing a robust Zod validation schema for an input field that handles product names. This schema needs to be able to adapt to various validation requirements, such as minimum/maximum length and the inclusion of special characters. B ...

The plugin "proposal-numeric-separator" was not found. Please make sure that there is a corresponding entry for it in the ./available-plugins.js file

{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "myProjects", "projects": { "uniqueApp": { "projectType": "web-app", "schematics": {}, "root": "", "sourceRoot": "src", ...

Having trouble deploying my MEAN stack application on Heroku due to a MongoNetworkError: unable to establish a connection to the

Encountering difficulty connecting to MongoDB for application deployment, receiving error message "MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017". What are potential issues ca ...

Aurelia TypeScript app experiencing compatibility issues with Safari version 7.1, runs smoothly on versions 8 onwards

Our team developed an application using the Aurelia framework that utilizes ES6 decorators. While the app works smoothly on Chrome, Firefox, and Safari versions 8 and above, it encounters difficulties on Safari 7.1. What steps should we take to resolve th ...

What is the best method for calculating the total of a column field within an array in Angular 9.1.9?

I am using Angular 9.1.9 and Html to work with a nested array field in order to calculate the total sum and display it in a row. Within my array list ('adherant'), I am aiming to sum up a specific column's values ({{ Total Amount }}) and pr ...

When using the ngFor directive, the select tag with ngModel does not correctly select options based on the specified

Issue with select dropdown not pre-selecting values in ngFor based on ngModel. Take a look at the relevant component and html code: testArr = [ { id : '1', value: 'one' }, { id : '2', ...

Issue with ng-bootstrap carousel: The property 'toArray' cannot be read as it is undefined in the NgbCarousel

Having trouble sliding to the next slide and encountering this error: ERROR TypeError: Cannot read property 'toArray' of undefined at NgbCarousel._getNextSlide (carousel.js:118) Does anyone have any insights on this? import { Component, In ...

Tips on searching for an entry in a database with TypeScript union types when the type is either a string or an array of strings

When calling the sendEmail method, emails can be sent to either a single user or multiple users (with the variable type string | string[]). I'm trying to find a more efficient and cleaner way to distinguish between the two in order to search for them ...

Storing the ngFor output in a variable with Angular 2

I've been exploring how to achieve this functionality using AngularJS and was successful with the following code: item in (filteredList.sup = (nozzles | rangeFilter: { flow: calc.q.tempFlow, pressure: calc.pressure } | orderBy: 'pressao')) ...

Angular 14's "rootItem" animation trigger was created with the following alerts: - The properties listed below are not animatable: overflow

Upon upgrading to Angular 14, I encountered this warning. The properties mentioned are not actually used in my animations. [Error in console][1] The animation triggers "rootItem" has been built with the following warnings: - The following provided propert ...

How can I retrieve user group information from Keycloak in my Angular application?

While it is common knowledge that Keycloak stores user information in local storage for easy access to username and email, I am curious about how to retrieve details regarding the group(s) a user is associated with. Can anyone provide insights on this? ...

Dynamic Type in Typescript Record

Looking for a way to attach types to record names in a class that returns a Record. The current code snippet is as follows: interface DataInterface { bar: number; foo: string; fooBar: boolean; } export class MyClass { public bar: number; p ...

Oops! An issue has occurred: Angular template is unable to read the property 'description' as it is undefined

This is my amazing service providing up-to-date information at your fingertips! fetchCurrentAffairs() : Observable<NewsData[]> { return this.httpClient.get<NewsData[]>(`${this.url}/latest`); } Introducing the brilliant component to brin ...

problems encountered when testing azure containerclient.listblobsbyhierarchy using sinon

I have developed a REST endpoint with the code "/files/lookup", which is designed to receive a query parameter folderPath and return a list of files with details, excluding content but including metadata. The endpoint connects to Azure Blob Stora ...

"Troubleshooting with Express.js: Why isn't the route-level middleware getting invoked

Currently, I am facing an issue with my Express.js and TypeScript API. Although the API is working smoothly, it seems that the middleware is not being triggered. The routing requests are successfully passed to the handler without involving the middleware a ...

Purpose of receiving a call without displaying in an angular testing scenario

I need help with some code I am working on. export class MyComponent implements OnInit { ngOnInit() { // Some code here this.method1(); // This part is getting executed. } method1() { console.log('method1'); // Bu ...