我有一个用于映射的日期数组。当尝试使用索引传递来自不同数组(名称)的项时,我得到未定义的值,因为"names"数组的条目数少于索引(日期)。
我有第三个数组,它应该是正确的格式(format):
let names = [
"chair",
"table",
"door",
"window",
"glass",
"wine",
"car",
"keys",
"dream",
"keyboard",
"vodka",
"pepsi",
"bag",
"ikea",
"mercedes",
"soprano"
];
let format = [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];
let dates = [
"2021-10-04T04:00:00.000Z",
"2021-10-05T04:00:00.000Z",
"2021-10-06T04:00:00.000Z",
"2021-10-13T04:00:00.000Z",
"2021-10-14T04:00:00.000Z",
"2021-10-15T04:00:00.000Z",
"2021-10-15T04:00:00.000Z",
"2021-10-17T22:00:00.000Z",
"2021-10-18T22:00:00.000Z",
"2021-10-19T22:00:00.000Z",
"2021-10-20T22:00:00.000Z",
"2021-10-21T22:00:00.000Z",
"2021-10-17T22:00:00.000Z",
"2021-10-18T22:00:00.000Z",
"2021-10-19T22:00:00.000Z",
"2021-10-20T22:00:00.000Z",
"2021-10-19T04:00:00.000Z",
"2021-10-20T04:00:00.000Z",
"2021-10-21T04:00:00.000Z",
"2021-10-22T04:00:00.000Z",
"2021-10-19T04:00:00.000Z",
"2021-10-20T04:00:00.000Z",
"2021-10-21T04:00:00.000Z",
"2021-10-25T04:00:00.000Z",
"2021-10-26T04:00:00.000Z",
"2021-10-27T04:00:00.000Z",
"2021-10-28T04:00:00.000Z",
"2021-10-29T04:00:00.000Z",
"2021-10-25T04:00:00.000Z",
"2021-10-26T04:00:00.000Z",
"2021-10-27T04:00:00.000Z",
"2021-10-28T04:00:00.000Z",
"2021-10-29T04:00:00.000Z",
"2021-11-01T04:00:00.000Z",
"2021-11-02T04:00:00.000Z",
"2021-11-03T04:00:00.000Z",
"2021-11-04T04:00:00.000Z",
"2021-11-05T04:00:00.000Z",
"2021-11-08T04:00:00.000Z",
"2021-11-09T04:00:00.000Z",
"2021-11-10T04:00:00.000Z",
"2021-11-01T04:00:00.000Z",
"2021-11-02T04:00:00.000Z",
"2021-11-03T04:00:00.000Z",
"2021-11-04T04:00:00.000Z",
"2021-11-05T04:00:00.000Z",
"2021-11-08T04:00:00.000Z",
"2021-11-09T04:00:00.000Z",
"2021-11-10T04:00:00.000Z",
"2021-11-11T04:00:00.000Z",
"2021-11-12T04:00:00.000Z",
"2021-11-11T04:00:00.000Z",
"2021-11-12T04:00:00.000Z",
"2021-11-13T04:00:00.000Z",
"2021-11-15T04:00:00.000Z",
"2021-11-16T04:00:00.000Z",
"2021-11-17T04:00:00.000Z",
"2021-11-18T04:00:00.000Z",
"2021-11-19T04:00:00.000Z",
"2021-11-16T04:00:00.000Z",
"2021-11-17T04:00:00.000Z",
"2021-11-18T04:00:00.000Z",
"2021-11-19T04:00:00.000Z",
"2021-11-20T04:00:00.000Z",
"2021-11-23T04:00:00.000Z",
"2021-11-24T04:00:00.000Z",
"2021-11-23T04:00:00.000Z",
"2021-11-24T04:00:00.000Z",
"2022-01-05T04:00:00.000Z",
"2022-01-06T04:00:00.000Z",
"2022-01-07T04:00:00.000Z",
"2022-01-10T04:00:00.000Z",
"2022-01-11T04:00:00.000Z",
"2022-01-12T04:00:00.000Z",
"2022-01-13T04:00:00.000Z",
"2022-01-14T04:00:00.000Z",
"2022-01-17T04:00:00.000Z",
"2022-01-18T04:00:00.000Z"
];
let app_multiple = dates.map(function combineTitleData(dataItem, index) {
return {
text: 'LR' + dates[index] + ': ' + names[index],
};
});
console.log(app_multiple);
我想要实现的是使用"格式"数组将遵循模式并使用该数组来构造映射。
格式包括:[3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];
我想在映射中使用"名称";这样的数组:
(chair) -> 3 times
(table) -> 3 times
(door) -> 1 time
(window) -> 5 times
(glass) -> 4 times
.... etc
那么输出就像这样:
[
{
"text": "LR2021-10-04T04:00:00.000Z: chair"
},
{
"text": "LR2021-10-05T04:00:00.000Z: chair"
},
{
"text": "LR2021-10-06T04:00:00.000Z: chair"
},
{
"text": "LR2021-10-13T04:00:00.000Z: table"
},
{
"text": "LR2021-10-14T04:00:00.000Z: table"
},
{
"text": "LR2021-10-15T04:00:00.000Z: table"
},
{
"text": "LR2021-10-15T04:00:00.000Z: door"
},
{
"text": "LR2021-10-17T22:00:00.000Z: window"
},
{
"text": "LR2021-10-18T22:00:00.000Z: window"
},
{
"text": "LR2021-10-19T22:00:00.000Z: window"
},
{
"text": "LR2021-10-20T22:00:00.000Z: window"
},
{
"text": "LR2021-10-21T22:00:00.000Z: window"
}
.... etc
.... etc
]
这是可能的吗?谢谢。
第一个选项是:
var numTimesUsed = 0;
var nameIndex = 0;
let app_multiple = dates.map(function combineTitleData(dataItem, index) {
if(format[nameIndex] == numTimesUsed) {
nameIndex++;
numTimesUsed = 0;
}
numTimesUsed++;
return {
text: 'LR' + dates[index] + ': ' + names[nameIndex],
};
});
第二个选项是(我将遍历名称映射而不是此选项的日期):
var dateIndex = 0;
let app_multiple = names.map(function combineTitleData(nameItem, index) {
var datesForName = [];
for(var i = 0; i < format[index]; i++) {
datesForName[i] = {
text: 'LR' + dates[dateIndex] + ': ' + names[index],
};
dateIndex++;
}
return datesForName;
});
你可能也可以使用一些数学方法来避免在第二个选项中使用额外的变量,但是只使用一个额外的变量更容易。