我正在做下面的工作。我认为这可能不可能,但如果可以,请分享。我正试图根据if语句更改this.c,而不是将data.filter反复写入3次以上。
if (chart === 'c1') { currentChart = this.c1; } else if (chart === 'c2') { currentChart = this.c2; } else if (chart === 'c3') { currentChart = this.c3; } currentChart = data.filter(o => {
像那样。 问题是没有将指针重新分配给data.filter。但是插入这个.c=data.filter,它指向这个.c#
if (chart === 'c1') { currentChart = this.c1; } else if (chart === 'c2') { currentChart = this.c2; } else if (chart === 'c3') { currentChart = this.c3; }
可以替换为
currentChart = this[chart];
或者更安全:
currentChart = ['c1', 'c2', 'c3'].includes(chart) ? this[chart] : null;