Home > Enterprise >  Why is this map not mapping the 1st and last elements when combining and transposing a dataset?
Why is this map not mapping the 1st and last elements when combining and transposing a dataset?

Time:06-21

I'm getting ranges from rows and columns and combining them into a tabular dataset, where dates in columns are brought into rows and reporting on the data becomes easier down the road.

Tanaike kindly provided a solution to it, but when I tweak it and apply it to a different situation, it's not getting the first and the last elements into the resulting array and I can't find out why - obviously, because I still don't know map(), flatMap() and so on...

These are the datasets:

const dates = ["01/31/2021","02/28/2021","03/31/2021","04/30/2021","05/31/2021","06/30/2021","07/31/2021","08/31/2021","09/30/2021","10/31/2021","11/30/2021","12/31/2021","01/31/2022","02/28/2022","03/31/2022","04/30/2022","05/31/2022","06/30/2022","07/31/2022","08/31/2022","09/30/2022","10/31/2022","11/30/2022","12/31/2022","01/31/2023","02/28/2023","03/31/2023","04/30/2023","05/31/2023","06/30/2023","07/31/2023","08/31/2023","09/30/2023","10/31/2023","11/30/2023","12/31/2023","01/31/2024","02/29/2024","03/31/2024","04/30/2024","05/31/2024","06/30/2024","07/31/2024","08/31/2024","09/30/2024","10/31/2024","11/30/2024","12/31/2024","01/31/2025","02/28/2025","03/31/2025","04/30/2025","05/31/2025","06/30/2025","07/31/2025","08/31/2025","09/30/2025","10/31/2025","11/30/2025","12/31/2025"]
const data = [[1,"Owner Test","CEO","Salary",283000,"","2021-01-31T08:00:00.000Z","2021-02-28T08:00:00.000Z",23583.333333333332,23583.333333333332,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[2,"Employee Test","Content Creator","Salary",37000,40,"2021-01-31T08:00:00.000Z","2021-10-31T07:00:00.000Z",3083.3333333333335,3083.3333333333335,3083.3333333333335,3083.3333333333335,3083.3333333333335,3083.3333333333335,3083.3333333333335,3083.3333333333335,3083.3333333333335,3083.3333333333335,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

Code snippet

const [, ...d] = dates;
const [id, name, position, workType, rate, hourWeek, dateHired, terminationDate, ...values] = data[0].map((_, c) => data.map(r => r[c]));
let res = d.flatMap((e, i) => id.map((f, j) => [e, f, name[j], position[j], workType[j], rate[j], hourWeek[j], dateHired[j], terminationDate[j], values[i][j]]));

The resulting array doesn't include the first date and the last one...

Appreciate any direction/help!

CodePudding user response:

In your script, how about the following modification?

From:

const [, ...d] = dates;

To:

const d = dates;
  • When I saw your sample value of dates, the 1st element is not empty. So, in this case, I thought that you might be able to use all values of dates.
  • Related