简化一个对象,我可以将这个数据创建为数组吗

Streamlining an object, can I create this data as an array?

本文关键字:创建 数据 数组 一个对象 我可以      更新时间:2023-09-26

我有一个包含一些数据的对象。第一个维度从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的存在