Home > Software design >  Why is my mapping returning too many entries?
Why is my mapping returning too many entries?

Time:10-29

I need to map an array of dates. Each one of these dates belongs to a group of dates (format array).

So let's say: Format array is: let format = [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];

That means the first three dates of the dates array are a group of dates, the next 3; the next one is a single date, the next 5... etc.

So my expecting output would be 16 items (same as format length). The output should be, for example: The first group of dates: Start: lowest date of that group End: highest date of that group

But my output is returning the same entries of date length.

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Thanks

CodePudding user response:

Splice will get the groups

const res = format.map(num => dates.splice(0,num))

Show code snippet

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)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

To get an array of objects of start and end (you asked for only start if no end) you can do this - I am assuming the date strings are sorted. If not, you can easily sort the strings with 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)

Show code snippet

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)
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Or same start and end if only one date:

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)

CodePudding user response:

Map always return the same number of input as the output, so if you use dates you cant expect to get format array length output. So we use the format as in input of the map to get array collection.

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);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related