简体   繁体   English

如何使用角度替换删除逗号

[英]How to remove the comma using the replace in angular

here's the code: https://stackblitz.com/edit/angular-w5ecbw?file=src/app/app.component.ts这是代码: https : //stackblitz.com/edit/angular-w5ecbw?file=src/app/app.component.ts

item.component.ts item.component.ts

ngOnInit() {
    const data = ['.23.2 ms','.23.001 ms', '.23.81 ms', '192,101.02 ms', '1291,291.02 ms'];
    for (let x = 0; x <= data.length; x++) {
      console.log(this.formatData(data[x].replace(/ .+/, '')))
    }
  }
  formatData(num: any) {
    const [num1, num2] = String(num).split('.');
    if (!num2) {
      return num1;
    } else {
      return `${num1}.${num2}`;
    }
  }

expected output should be:预期输出应该是:

.23.2
.23.001
    .23.81
    192101.02
    1291291.02

What I want to do here is to remove the comma.我想在这里做的是删除逗号。

Try this.尝试这个。

 const data = ['.23.2 ms','.23.001 ms', '.23.81 ms', '192,101.02 ms', '1291,291.02 ms']; const result = data.map( item => item.replace(/,|[^0-9\\.]|/g, "")); console.log(result.join(", "));

你可以像这样删除它:

string.replace(',', '');

User map , split , replace and join methods.用户mapsplitreplacejoin方法。

 const data = [ ".23.2 ms", ".23.001 ms", ".23.81 ms", "192,101.02 ms", "1291,291.02 ms" ]; updated = data.map(x => x.split(" ms")[0].replace(",", "")).join(''); console.log(updated);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM