首页 > 解决方案 > 声明具有相同值的对象属性

问题描述

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

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'],

    };

我想知道如何简化这个声明部分。

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

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

我有什么办法吗?

标签: javascriptarraysobject

解决方案


您可以创建可重用的变量来干燥您的代码:

const stateValues = ['state', 'command']
const frameValues = ['frame', 'command', 'damage', 'range', 'hitframe'] 

const arr: any = {
        Combo: stateValues,
        Extrahit: stateValues,
        Punish: frameValues,
        standing: frameValues,
        up: frameValues,
    };

推荐阅读