如何最小化打字稿功能以少花钱多办事

How to minimize typescript function to do more for less

我有这个功能:

private sectionIndex: number; <--- set above my constructor.

sections 对象的项目是:

public sections = {
  pi: [ **<-- Want to remove**
    {title: 'Employee Information'},
    {title: 'Address'},
    {title: 'Summary'},
  ],
  ee: [ **<-- Want to remove**
    {title: 'Employee Eligibility Information'},
    {title: 'Summary'},
  ],
  pd: [ **<-- Want to remove**
    {title: 'Payroll Information'},
    {title: 'Summary'},
  ],
  es: [ **<-- Want to remove**
    {title: 'Esignature'},
    {title: 'Summary'},
  ]
};

想这样合并:

public sections = {
  s: [
    {title: 'Employee Information'},
    {title: 'Address'},
    {title: 'Employee Eligibility Information'},
    {title: 'Summary'}, <-- Common between all...
    {title: 'Payroll Information'},
    {title: 'Esignature'}
  ]
};

这是我想要最小化的函数:

public somefunction() {

    let result = true;

    if (this.sectionIndex === this.sections.pi.length - 1 ||
      this.sectionIndex === this.sections.pd.length - 1 ||
      this.sectionIndex === this.sections.ee.length - 1 ||
      this.sectionIndex === this.sections.es.length - 1) {
      result = false;
      console.log('There are no more sections');
    } else {
      this.sectionIndex++;

    if (this.sections.pi.length - 1) {
        console.log('First section is ' + 
this.sections.pi[this.sectionIndex].title + ' which is ' + 
this.sections.pi[this.sectionIndex].value);
      } else if (this.sections.pd.length - 1) {
        console.log('First section is ' + 
this.sections.pd[this.sectionIndex].title + ' which is ' + 
this.sections.pd[this.sectionIndex].value);
      } else if (this.sections.pi.length - 1) {
        console.log('First section is ' + 
this.sections.ee[this.sectionIndex].title + ' which is ' + 
this.sections.ee[this.sectionIndex].value);
      } else if (this.sections.es.length - 1) {
        console.log('First section is ' + 
this.sections.es[this.sectionIndex].title + ' which is ' + 
this.sections.es[this.sectionIndex].value);
      }
    }
    return result;
 }

我想将上面的代码最小化为更可口的代码,而我不必执行 "OR's" if then else, else if's, blah, blah, blah...

此外,这些部分(目前是硬编码的)并不总是相同的。可能多,也可能少。

相似的是摘要部分。摘要将出现在所有部分

我的问题是,如何组合sections对象并删除仅使用子项遍历的"pi, ee, pd, and es?"?

要合并这些部分,您可以 flatten the object values。然后 要删除重复项,您可以使用 set 保留已添加的条目:

const sections = {
  pi: [ 
    {title: 'Employee Information'},
    {title: 'Address'},
    {title: 'Summary'},
  ],
  ee: [ 
    {title: 'Employee Eligibility Information'},
    {title: 'Summary'},
  ],
  pd: [ 
    {title: 'Payroll Information'},
    {title: 'Summary'},
  ],
  es: [ 
    {title: 'Esignature'},
    {title: 'Summary'},
  ]
};

const combined = Object.values(sections).flat();
const addedTitles = new Set();
const noDuplicates = [];
combined.forEach(item => {
  if (!addedTitles.has(item.title)){
    noDuplicates.push(item);
    addedTitles.add(item.title)
  }
});

const result = { s: noDuplicates };
console.log(result);

如果您不希望列表很大,您可以使用 find 检查项目是否已经在列表中(不添加标题集)。类似于:

const combined = Object.values(sections).flat().reduce((result, item) => {
    if (!result.find(i => i.title === item.title)) {
        result.push(item);
    }
    return result;
}, []);