In TypeScript I want distinct values from comma separated duplicate string:
this.Proid = this.ProductIdList.map(function (e) { return e.ProductId;}).join(',');
this.Proid = "2,5,2,3,3";
And I need :
this.Proid = "2,5,3";
In TypeScript I want distinct values from comma separated duplicate string:
this.Proid = this.ProductIdList.map(function (e) { return e.ProductId;}).join(',');
this.Proid = "2,5,2,3,3";
And I need :
this.Proid = "2,5,3";
This can be simply done with with ES6,
var input = [2,5,2,3,3];
var test = [ ...new Set(input) ].join();
DEMO
var input = [2,5,2,3,3];
var test = [ ...new Set(input) ].join();
console.log(test);
EDIT
For ES5 and below, you can try,
var input = [2,5,2,3,3];
var test = Array.from(new Set(input).values()).join();
console.log(test);
One possible solution:
this.ProductIdList = ["2","5","2","3","3"]
const tab = this.ProductIdList.reduce((acc, value) => {
return !acc.includes(value) ? acc.concat(value) : acc
}, []).join(',');
console.log(tab) //"2,5,3"
You can do in one line too:
this.ProductIdList = ["2","5","2","3","3"]
const tab = this.ProductIdList.reduce((acc, value) => !acc.includes(value) ? acc.concat(value) : acc, []).join(',');
console.log(tab) //"2,5,3"