简化一个对象,我可以将这个数据创建为数组吗
Streamlining an object, can I create this data as an array?
我有一个包含一些数据的对象。第一个维度从1到12,代表一年中的几个月。在其中,我有特定的ID,这些ID在当月是"真实的":
var monthData = {
1: { 219: true, 474: true },
2: { 219: true, 391: true },
3: { 219: true },
...
};
要使用此数据,我搜索month
(值1-12),然后搜索当月的ID
(例如391)。如果ID
在该月出现,则I return true;
return !!monthData[month][ID];
然而,当看到我拥有的完整数据时,似乎有很多重复。例如,我每个月都在重复219: true
。我还在每个ID之后重复true
-实际上只是ID
的存在使其成为现实,所以我觉得这可以改进。
我可以将其存储为数组数据吗?是否有一种简单的方法来迭代这个并返回我想要的数据?
功能如下:
findIDByMonth : function (month,ID) {
var monthData = {
1: { 219: true, 474: true },
2: { 219: true, 474: true },
3: { 219: true, 474: true },
4: { 219: true, 296: true, 391: true, 474: true, 578: true, 917: true, 987: true, 1036: true },
5: { 219: true, 296: true, 391: true, 474: true, 578: true, 917: true, 987: true, 1036: true },
6: { 219: true, 391: true, 474: true, 578: true, 834: true, 917: true, 987: true, 1036: true },
7: { 219: true, 251: true, 391: true, 474: true, 578: true, 834: true, 917: true, 987: true, 1036: true },
8: { 219: true, 251: true, 391: true, 474: true, 526: true, 578: true, 834: true, 917: true, 987: true, 1036: true, 1092: true },
9: { 219: true, 251: true, 296: true, 391: true, 474: true, 526: true, 578: true, 897: true, 917: true, 987: true, 1036: true, 1092: true },
10: { 219: true, 251: true, 391: true, 474: true, 526: true, 578: true, 897: true, 917: true, 987: true, 1036: true, 1092: true },
11: { 219: true, 474: true, 987: true, 1036: true },
12: { 219: true, 474: true }
};
return !!monthData[month][ID];
}
您可以在函数中添加类似的内容。我认为数组可以是一个很好的选择可读性,正如你提到的。我保持你的外部对象不变,因为它似乎不需要改变为一个数组,因为它不会减少你的代码长度,它更清楚你选择哪个月。
var monthData = {
1: [219, 474],
2: [219, 391],
...
};
return !!monthData[month].includes(ID);
您可以将所有数据存储为数组的数组,然后使用indexOf()
来搜索数据。
function findIDByMonth(month,ID) {
var monthData = [
[219, 474],
[219, 474],
[219, 474],
[219, 296, 391, 474, 578, 917, 987, 1036],
[219, 296, 391, 474, 578, 917, 987, 1036],
[219, 391, 474, 578, 834, 917, 987, 1036],
[219, 251, 391, 474, 578, 834, 917, 987, 1036],
[219, 251, 391, 474, 526, 578, 834, 917, 987, 1036, 1092],
[219, 251, 296, 391, 474, 526, 578, 897, 917, 987, 1036, 1092],
[219, 251, 391, 474, 526, 578, 897, 917, 987, 1036, 1092],
[219, 474, 987, 1036],
[219, 474]];
return monthData[month - 1].indexOf(ID) !== -1;
}
对于几个月内重复出现的id,除非它是静态数据,否则你无能为力。
findIDByMonth : function (month,ID) {
var monthData = [
[ 219, 474 ],
[ 219, 474 ],
[ 219, 474 ],
[ 219, 296, 391, 474, 578, 917, 987, 1036 ],
[ 219, 296, 391, 474, 578, 917, 987, 1036 ],
[ 219, 391, 474, 578, 834, 917, 987, 1036 ],
[ 219, 251, 391, 474, 578, 834, 917, 987, 1036 ],
[ 219, 251, 391, 474, 526, 578, 834, 917, 987, 1036, 1092 ],
[ 219, 251, 296, 391, 474, 526, 578, 897, 917, 987, 1036, 1092 ],
[ 219, 251, 391, 474, 526, 578, 897, 917, 987, 1036, 1092 ],
[ 219, 474, 987, 1036 ],
[ 219, 474 ]
];
return monthData[month-1].includes(ID);
}
当您说要将数据存储为数组时,并不清楚您在寻找什么。
如果你想完全平坦化你的数据,你可以创建一个字符串数组,以month-id
的形式,所以它会变成这样:
var monthData=["1-219", "1-474", "2-219", "2-474"....];
则可以检查数组是否包含月-id组合
return monthData.includes(month+"-"+ID);
如果你的目的是检查特定项是否存在那么你的原始表示是最好的因为访问对象的属性是在常数时间内完成的而迭代数组更昂贵(线性时间,但通常是优化的)所以即使它有点丑,你的数据表示是最有效的如果你的目的是检查特定id的存在
相关文章:
- 使用AJAX创建数据列表
- 如何为 Flot JS 动态创建数据集
- 指定单击以从数据库中动态创建数据
- 如何在 C# 中使用多个嵌套 JSON 对象创建数据表
- 调用成功后重新创建数据,AJAX
- 标签在创建数据通道中的作用是什么以及如何正确使用它
- 使用 C# 在控制器中创建数据结构并将其传递到视图中
- 从上传的 csv 数据创建数据表列定义
- 为谷歌图表创建数据数组
- 如何在html中创建数据模板
- 在mongo中检查数据并创建数据的有效方法'不存在
- 如何为Breeze创建数据绑定适配器
- 谷歌图表-从Ajax创建数据
- 使用jQuery和Bootstrap创建数据表,而不是使用基本的HTML
- 我如何使用Ember创建数据驱动的文本输入
- 如何为一艘船创建数据模型
- 如何在chart.js中动态创建数据集
- 动态创建数据uri
- 使用Javascript为CakePHP创建数据数组
- 通过变量中的json数据在js中创建数据表