我面临一个问题我有一个目标

{
    "Draft": 19,
    "Process Model QA review in progress": 22,
    "Process Model QA Rejected": 17,
    "Process Model QA Approved": 13,
    "Process Owner review in progress": 28,
    "Approved": 22,
    "Opt-out": 3,
    "Process Owner Rejected": 4,
    "Process Owner Approved": 10,
    "Soln. Architect review in progress": 4,
    "Soln. Architect Rejected": 4,
    "Cancelled": 10
}

constructor(@Self() private titleCasePipe: TitleCasePipe)

 Object.entries(obj).map(elem => {
    arr.push({'id': elem[0], 'name': this.titleCasePipe.transform(elem[0]) + " (" + elem[1] + ")"})
 })
 
 console.log(arr)

我需要大写的所有单词的第一个字母的对象键.因此,我使用了titleCase管道来实现它,但它也大写QAQa—(过程模型Qa审查正在进行).

我不确定是否有任何方法跳过QA和资本化其他?

或 由于HTTP API有obj个数据到达,我可以使用map操作符来大写吗?

推荐答案

titlecase的代码非常直接,没有自定义!

// #docregion
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'titlecase', standalone: true, pure: true })
/** Transform to Title Case: uppercase the first letter of the words in a string. */
export class TitleCasePipe implements PipeTransform {
  transform(input: string): string {
    return input.length === 0
      ? ''
      : input.replace(/\w\S*/g, (txt) => txt[0].toUpperCase() + txt.slice(1).toLowerCase());
  }
}

只是go 后转换,做一个正则表达式替换白名单关键字!

下面的例子!

import { CommonModule, TitleCasePipe } from '@angular/common';
import { Component, Self } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule],
  providers: [TitleCasePipe],
  template: `
    <h1>Hello from {{ name }}!</h1>
    <a target="_blank" href="https://angular.dev/overview">
      Learn more about Angular
    </a>
  `,
})
export class App {
  name = 'Angular';

  constructor(@Self() private titleCasePipe: TitleCasePipe) {}

  ngOnInit() {
    const obj = {
      Draft: 19,
      'Process Model QA review in progress': 22,
      'Process Model QA Rejected': 17,
      'Process Model QA Approved': 13,
      'Process Owner review in progress': 28,
      Approved: 22,
      'Opt-out': 3,
      'Process Owner Rejected': 4,
      'Process Owner Approved': 10,
      'Soln. Architect review in progress': 4,
      'Soln. Architect Rejected': 4,
      Cancelled: 10,
    };

    const arr: any = [];
    Object.entries(obj).map((elem) => {
      arr.push({
        id: elem[0],
        name: this.safeTransform(elem[0]) + ' (' + elem[1] + ')',
      });
    });

    console.log(arr);
  }

  safeTransform(elem: any) {
    let output = this.titleCasePipe.transform(elem);
    // whitelist
    ['QA'].forEach((regexKey) => {
      var re = new RegExp(regexKey, 'gi');
      output = output.replace(re, regexKey);
    });
    return output;
  }
}

bootstrapApplication(App);

Stackblitz Demo

Typescript相关问答推荐

错误:note_modules/@types/note/globals.d.ts:72:13 -错误TS 2403:后续变量声明必须具有相同的类型

如何传递基于TypScript中可变类型的类型?

从接口创建类型安全的嵌套 struct

为什么缩小封闭内部不适用于属性对象,但适用于声明的变量?

如何使类型只能有来自另一个类型的键,但键可以有一个新值,新键应该抛出一个错误

将值添加到具有不同类型的对象的元素

在Angular中注入多个BrowserModules,但在我的代码中只有一个

在键和值为ARGS的泛型函数中未正确推断类型

隐式键入脚本键映射使用

如何将所有props传递给React中的组件?

对未到达受保护路由的路由环境做出react

使用订阅时更新AG网格中的行

找不到财产的标准方式?

使用条件类型的类型保护

对象类型的TypeScrip隐式索引签名

基于泛型的条件接口键

为什么TypeScrip不能解析对象析构中的赋值?

tailwind css未被应用-react

Typescript泛型-键入对象时保持推理

带有 Select 器和映射器的Typescript 泛型