K
K
Konstantin2020-08-15 01:28:00
JavaScript
Konstantin, 2020-08-15 01:28:00

How can the function with duplicates be simplified and improved?

The code is the following:

updateResultat(node: any, field: DocumentField): void {
        if (field.fieldvalue == 1) {
            this.dfs_change(node.children, { name: 'visible', value: true }, { name: 'tag', value: 'OPERATORTYPE' });
        } else {
            this.dfs_change(node.children, { name: 'visible', value: false }, { name: 'tag', value: 'OPERATORTYPE' });
        }

        if (field.fieldvalue == 2) {
            this.dfs_change(node.children, { name: 'visible', value: true }, { name: 'tag', value: 'MAILTYPE' });
        } else {
            this.dfs_change(node.children, { name: 'visible', value: false }, { name: 'tag', value: 'MAILTYPE' });
        }

        if (field.fieldvalue == 3) {
            this.dfs_change(node.children, { name: 'visible', value: true }, { name: 'tag', value: 'MAILADDRESS' });
        } else {
            this.dfs_change(node.children, { name: 'visible', value: false }, { name: 'tag', value: 'MAILADDRESS' });
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-08-15
@Junart1

[
  [ 1, 'OPERATORTYPE' ],
  [ 2, 'MAILTYPE' ],
  [ 3, 'MAILADDRESS' ],
].forEach(([ fieldValue, value ]) => {
  this.dfs_change(
    node.children,
    {
      name: 'visible',
      value: field.fieldvalue === fieldValue,
    },
    {
      name: 'tag',
      value,
    }
  );
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question