通过三段式判断,动态初始化数组

 初始化数组时,有时候不确定是否存在某个元素,数组的长度不确定,需要依据相应值来判断。

例如:

a = [1, 2, ...(flag ? [] : [3]) ]

// 当flag为true时,a = [1, 2];

// 当flag为false时,a = [1, 2, 3];

    this.columnDefs = [
      {
        field: 'type',
        headerName: this.translateService.instant('dj-default-类别'),
        hide: false,
        width: 95,
        valueFormatter: this.typeTranslate.bind(this),
      },
      ...(this.validateForm.get('open_stage').value !== '0'?[]:[
        {
          field: 'bom_seq',
          headerName: this.translateService.instant('dj-default-BOM序号'),
          hide: false,
          width: 90,
        }
      ]),
      {
        field: 'component_no',
        headerName: this.translateService.instant('dj-default-元件品号'),
        hide: false,
        width: 130,
      },
    ];

猜你喜欢

转载自blog.csdn.net/qq_38679823/article/details/128456143
今日推荐