This might be a basic issue that I am unable to fix. I have an initial array values received from an API something like this:
const primaryArray = [
{
startDate: '01-01-2021',
endDate: '11-01-2021',
refs: [
{
refId: "FL013",
year: "2021"
}
]
},
{
startDate: '01-02-2021',
endDate: '28-02-2021',
refs: [
{
refId: "FL013",
year: "2021"
}
]
},
{
startDate: '03-05-2020',
endDate: '10-06-2020',
refs: [
{
refId: "FL013",
year: "2020"
}
]
},
{
startDate: '25-03-2019',
endDate: '25-04-2019',
refs: [
{
refId: "FL013",
year: "2019"
}
]
}
];
Since two of the objects of year 2021 share the same reference key as 'FL013', I want to convert them into the form as this with all objects separated out from array:
{
2021: {
'year': 2021,
'refIds': ['FL013', 'FL013'],
'duration': {
'FL013': [
{
startDate: '01-01-2021',
endDate: '11-01-2021'
},
{
startDate: '01-02-2021',
endDate: '28-02-2021',
}
]
}
}
},
{
2020: { year: 2020, 'refIds': ['FL013'], 'duration': { 'FL013': {
startDate: '03-05-2020',
endDate: '10-06-2020',
}
}
},
{
2019: { year: 2019, 'refIds': ['FL013'], 'duration': { 'FL013': {
startDate: '25-03-2019',
endDate: '25-04-2019',
}
}
}
I tried to use JS reduce for the same, but the array creation for year with two dates objects keeps throwing me an error.
This is what I tried to achieve so far:
const flatArray = primaryArray && primaryArray.reduce(
(acc, {startDate, endDate, refs}, currentIndex) => {
const year = refs && refs[0] && refs[0].year;
const refId = refs && refs[0] && refs[0].refId;
if (
year &&
refId &&
startDate &&
endDate &&
!Object.prototype.hasOwnProperty.call(acc, year)
) {
acc[year] = {
year,
refIds: [refId],
duration: {
[refId]: {
startDate,
endDate,
},
},
};
} else {
if (year) {
acc[year].refIds.push(refId);
acc[year]['duration'][refId] = {
startDate,
endDate,
};
}
}
return acc;
},{});
I modified my above code using two approaches to achieve the exact result: first, to push the dates object as received in the accumlator
acc[year]['duration'][refId].push({ startDate, endDate }).
This throws error:
Uncaught TypeError: acc[year].duration[refId].push is not a function
or second, direct apply [] notation (which I understand is not applicable).
Here is the working link for the same: https://onecompiler.com/javascript/3wwzz3f4s
Any help to resolve the same, highly appreciated.