man*_*esh 0 typescript angular
在 TypeScript 中,我想要与逗号分隔的重复字符串不同的值:
this.Proid = this.ProductIdList.map(function (e) { return e.ProductId;}).join(',');
this.Proid = "2,5,2,3,3";
Run Code Online (Sandbox Code Playgroud)
我需要:
this.Proid = "2,5,3";
Run Code Online (Sandbox Code Playgroud)
这可以用 ES6 简单地完成,
var input = [2,5,2,3,3];
var test = [ ...new Set(input) ].join();
Run Code Online (Sandbox Code Playgroud)
演示版
var input = [2,5,2,3,3];
var test = [ ...new Set(input) ].join();
Run Code Online (Sandbox Code Playgroud)
编辑
对于 ES5 及以下版本,您可以尝试,
var input = [2,5,2,3,3];
var test = [ ...new Set(input) ].join();
console.log(test);Run Code Online (Sandbox Code Playgroud)
一种可能的解决方案:
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"
Run Code Online (Sandbox Code Playgroud)
你也可以在一行中完成:
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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3456 次 |
| 最近记录: |