I am looking for unique values from a duplicate string that is separated by commas in TypeScript

Using TypeScript, I am trying to extract unique values from a list of comma-separated duplicate strings:

this.Proid = this.ProductIdList.map(function (e) { return e.ProductId;}).join(',');
this.Proid = "2,5,2,3,3";

The desired output is:

this.Proid = "2,5,3";

Answer №1

Using ES6, achieving this task is quite straightforward,

var numbers = [2,5,2,3,3];
var uniqueNumbers = [ ...new Set(numbers) ].join();

SEE IT IN ACTION

var numbers = [2,5,2,3,3];
var uniqueNumbers = [ ...new Set(numbers) ].join();
console.log(uniqueNumbers);

MODIFY

If you are working with ES5 or older versions, you can consider using the following approach,

var numbers = [2,5,2,3,3];
var uniqueNumbers = Array.from(new Set(numbers).values()).join();
console.log(uniqueNumbers);

Answer №2

Here is a potential answer:

let uniqueProducts = ["2","5","2","3","3"]
const uniqueList = uniqueProducts.reduce((acc, value) => {
    return !acc.includes(value) ? acc.concat(value) : acc
}, []).join(',');

console.log(uniqueList) //"2,5,3"

You could also achieve this in a single line:

let uniqueProducts = ["2","5","2","3","3"]
const uniqueList = uniqueProducts.reduce((acc, value) => !acc.includes(value) ? acc.concat(value) : acc, []).join(',');

console.log(uniqueList) //"2,5,3"

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

Updating a service component in Angular 6

Utilizing Angular 6, I find myself in need of updating the component whenever a service method is called. Within my code, there exists a variable agent that stores the selected agent from the HTML. This variable is accessed from multiple components and can ...

Managing a click event with an element in React using TypeScript

Hey there, I'm pretty new to TypeScript and React. I have this event where I need to identify the element that triggered it so I can move another element close to it on the page. However, I'm facing some challenges trying to make it work in React ...

Bringing in Chai with Typescript

Currently attempting to incorporate chai into my typescript project. The javascript example for Chai is as follows: var should = require('chai').should(); I have downloaded the type definition using the command: tsd install chai After refere ...

What is the best way to forward all URLs to one central page?

I've recently started working with Angular and I'm currently developing a web app project using Angular 9. I could really use your help with this. My goal is to have a dynamic URL structure for the web app, such as https://www.myMainURL.com/***, ...

How can I find the "types" specific to modules within the "firebase" library?

I have a question that applies to various scenarios, with Firebase serving as an example. When working on my react project, I find myself wanting to import firebase from "@firebase/app", which is logical. However, if I want the const locationRef ...

Exploring the ngCookies (AngularJS) functionality within Angular versions 4 and 5

Can you explain how to use $cookies in AngularJS within Angular 4/5? Here's an example using AngularJS: let app = angular.module('myApp', ['ngCookies']); app.controller('MainController', MainController); MainController. ...

Tips for migrating an AngularJS application to Angular

My current project involves implementing a basic search function using AngularJS (link). I want to integrate this feature into an existing Angular application. To do this, I created a new Angular app and transferred the view to app.component.html. <hea ...

What could be causing the Angular Material Dialog component to not display properly in Google Chrome?

I encountered an issue with the dialog box when testing it in different browsers. It seemed to work fine in Firefox, but in Chrome, the click event only displayed a small empty dialog box without any content or buttons. This problem arose while I was follo ...

Angular2: Exploring the Differences Between Observable.fromEvent and Button Click

As I work with my code, I have noticed that I am using both <button (click)="callsomefucntion" /> and Observable.fromEvent<MouseEvent>(button.nativeElement.'click') interchangeably. I am curious to understand the distinction between ...

What is the best approach to incorporate Column Reordering in react-data-grid?

Within my REACT application, I have incorporated the npm package react-data-grid. They offer a sample showcasing Column Reordering on their website. I wish to replicate this functionality in my own code. Upon reviewing their source code, I am puzzled abou ...

Setting up OpenID configuration in angular-oauth2-oidc to bypass authentication for certain addresses

In my Angular project, I implemented OpenID authentication using the angular-oauth2-oidc project. However, I need to disable authentication for certain routes. To achieve this, I start the code flow in the main component and bootstrap it in the main modu ...

The origin of the Angular img src becomes blurred when invoking a function

I want to dynamically change the image src by calling a function that returns the image path. However, when I attempt to do so using the code below, the image element displays as <img src(unknown)/> component.ts: getMedia(row) { this.sharedData ...

Leverage the VTTCue Object in an Angular2 project using Typescript

I am looking to dynamically load subtitles onto a video. let subs:TextTrack = video.addTextTrack('subtitles'); for (let dataSrt of dataSrts) { let cue: any = new VTTCue( dataSrt['startTime'], da ...

Encountered an error of 'npm ERR! invalid semver' when attempting to release an npm package

npm ERR! code EBADSEMVER npm ERR! invalid semver: npm ERR! Check out the full log of this run here: I attempted to reinstall node and semver, but unfortunately it did not resolve the issue. ...

What is the best way to update this payload object?

Currently, I'm developing a route and aiming to establish a generic normalizer that can be utilized before storing user data in the database. This is the function for normalization: import { INormalizer, IPayloadIndexer } from "../../interfaces/ ...

What is the best way to interact with the member variables and methods within the VideoJs function in an Angular 2 project

Having an issue with accessing values and methods in the videojs plugin within my Angular project. When the component initializes, the values are showing as undefined. I've tried calling the videojs method in ngAfterViewInit as well, but still not get ...

What is the way to assign a variable to ngClass in Angular?

I'm currently working on creating modals that will display different content based on which button is clicked. Each button should trigger a unique modal to appear, each with its own specific content inside. When a button is clicked, the 'active&a ...

ASP.Net Core 3.1 server receives a blank user in JWT Bearer token

Today, I've been working on integrating JSON Web Token information with HttpContext.User using the Microsoft.AspNetCore.Authentication.JwtBearer library. The Issue: Whenever I make a request to the server, I can access functions with the [Authorize] ...

What could be causing my vis.js network's node hover popups to not function properly?

I've encountered an issue where, despite adding the 'title' property to my node objects, the pop up window with the title content doesn't appear when I hover over a node. Here are the options I've chosen and how I've set up m ...

Angular Material Select Dropdown that emits the entire object item, rather than just the value

When using the Angular Material select dropdown API, only a value is emitted. Both [(ngModel)] and (selectionChange) will only emit a single member, not the entire object's data fields. For example, it will only emit something like food.value = 2, wi ...