声明具有相同值的Object属性



在我的对象中,有几个属性的值都相同,密钥不同

const arr: any = {
Combo: ['state', 'command'],
Extrahit: ['state', 'command'],
Punish: ['frame', 'command', 'damage', 'range', 'hitframe'],
standing: ['frame', 'command', 'damage', 'range', 'hitframe'],
up: ['frame', 'command', 'damage', 'range', 'hitframe'],
};

我想知道如何简化这个申报部分。

我发现的是使用迭代函数。但这种方式似乎不适合我的问题。

我正在寻找提高代码可读性的方法。

我有什么办法吗?

您可以创建可重复使用的变量来清空代码:

const stateValues = ['state', 'command']
const frameValues = ['frame', 'command', 'damage', 'range', 'hitframe'] 
const arr: any = {
Combo: stateValues,
Extrahit: stateValues,
Punish: frameValues,
standing: frameValues,
up: frameValues,
};

最新更新