我需要映射一組日期。這些日期中的每一個都屬于一組日期(格式陣列)。
所以讓我們說:格式陣列是: let format = [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];
這意味著日期陣列的前三個日期是一組日期,接下來是 3 個;下一個是單個日期,接下來的 5 個……等等。
所以我期望的輸出是 16 個專案(與格式長度相同)。輸出應該是,例如:第一組日期:開始:該組的最低日期結束:該組的最高日期
但是我的輸出回傳了相同的日期長度條目。
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"
];
var numTimesUsed = 0;
var nameIndex = 0;
let app_multiple = dates.map(function combineTitleData(dataItem, index) {
if (format[nameIndex] == numTimesUsed) {
nameIndex ;
numTimesUsed = 0;
}
numTimesUsed ;
let end = new Date(dates[nameIndex]);
end.setDate(end.getDate() parseInt(format[nameIndex]) - 1);
return {
start: dates[nameIndex],
end: end
};
});
//
console.log(app_multiple);
謝謝
uj5u.com熱心網友回復:
Splice將獲得組
const res = format.map(num => dates.splice(0,num))
顯示代碼片段
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" ];
const res = format.map(num => {
const arr = dates.splice(0,num)
const start = arr.shift(); // take the first
const end = arr.length === 0 ? start : arr.pop(); // take the last if there
return { start, end }
})
console.log(res)
要獲取開始和結束的物件陣列(如果沒有結束,您只要求開始),您可以執行此操作 - 我假設日期字串已排序。如果沒有,您可以輕松地對字串進行排序dates.sort()
const res = format.map(num => {
const arr = dates.splice(0, num)
const start = arr.shift();
const end = arr.length === 0 ? "" : arr.pop();
return end ? { start, end } : { start }
})
console.log(res)
顯示代碼片段
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" ];
const res = format.map(num => {
const arr = dates.splice(0, num)
const start = arr.shift();
const end = arr.length === 0 ? "" : arr.pop()
return end ? { start, end } : { start }
})
console.log(res)
或者相同的開始和結束,如果只有一個日期:
const res = format.map(num => {
const arr = dates.splice(0, num)
const start = arr.shift();
const end = arr.length === 0 ? start : arr.pop();
return { start, end }
})
console.log(res)
uj5u.com熱心網友回復:
Map始終回傳與輸出相同數量的輸入,因此如果您使用日期,則不能期望獲得格式陣列長度輸出。所以我們使用地圖輸入中的格式來獲取陣列集合。
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"
];
// convert string into dates for comparison
const realDates = dates.map(d => new Date(d));
// use map on format since, map always return the same number of input array length, so that was ur clue to use format instead of dates
const dateGroup = format.map(num => realDates.slice(0,num));
// now you only need to iterate over the array to return the new form
let app_multiple = dateGroup.map(function createNewObject(singleDateCollection, index) {
return {
start: new Date(Math.min.apply(null,singleDateCollection)),
end: new Date(Math.max.apply(null,singleDateCollection))
};
});
//
console.log(app_multiple);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/340807.html
標籤:javascript 数组
下一篇:C帶有列印和陣列的反向數字
