javascript - 声明具有相同值的对象属性
问题描述
在我的对象中,有几个属性值都相同而键不同
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,
};
推荐阅读
- python - 问题 - 处理后将文件写回本地系统
- html - IIS URL重写不附加查询字符串
- sql - Oracle/PL-SQl:将查询结果传递给另一个更新
- java - java语言中的字符串与字符串?
- python - 在 Windows 10 中双击运行 .py 文件
- stripe-payments - 如何在结帐前检查 Stripe 促销代码是否对产品有效
- jenkins-pipeline - odoo psycopg2.ProgrammingError:列 res_partner.field_name 不存在
- python-3.x - Python Pandas:按一列分组,仅在另一列中聚合,但取相应数据
- mysql - 我如何从大 mysql 表中获取每天的最后一行
- php - 为学校项目设置 SSO shibolleth