Seeking guidance on enhancing this method to eliminate the need for a workaround.
I have two separate Object Arrays containing JSON data. The first array consists of all dates within a specific range, while the second array is derived from a SQL query, potentially lacking some months.
The desired output should be a sorted list of all months with a matterCount value assigned. However, when I utilize the function below, I encounter an issue where the matterCount is missing for months not present in the list.
I believe that there might be a small code snippet missing from the function causing this problem, but I am uncertain about the exact solution which led me here for help.
Although I have a workaround available on JSFiddle, I prefer cleaner and clearer code.
let x = [{labelDate: "Jun 2013"},
{labelDate: "Jul 2013"},
{labelDate: "Aug 2013"},
{labelDate: "Sep 2013"},
{labelDate: "Oct 2013"},
{labelDate: "Nov 2013"},
{labelDate: "Dec 2013"},
{labelDate: "Jan 2014"},
{labelDate: "Feb 2014"},
{labelDate: "Mar 2014"},
{labelDate: "Apr 2014"},
{labelDate: "May 2014"}];
let y = [{labelDate: "Jun 2013", matterCount: "1"},
{labelDate: "Jul 2013", matterCount: "2"},
{labelDate: "Aug 2013", matterCount: "2"},
{labelDate: "Sep 2013", matterCount: "10"},
{labelDate: "Oct 2013", matterCount: "1"},
{labelDate: "Nov 2013", matterCount: "1"},
{labelDate: "Feb 2014", matterCount: "1"},
{labelDate: "Apr 2014", matterCount: "17"},
{labelDate: "May 2014", matterCount: "21"}];
const merge = (x, y) =>
x.map(xItem => ({
...y.find(yItem => yItem.labelDate === xItem.labelDate && Item),
...xItem
}));
Actual outcome
merge = [{labelDate: "Jun 2013", matterCount: "1"},
{labelDate: "Jul 2013", matterCount: "2"},
{labelDate: "Aug 2013", matterCount: "2"},
{labelDate: "Sep 2013", matterCount: "10"},
{labelDate: "Oct 2013", matterCount: "1"},
{labelDate: "Nov 2013", matterCount: "1"},
{labelDate: "Dec 2013"},
{labelDate: "Jan 2014"},
{labelDate: "Feb 2014", matterCount: "1"},
{labelDate: "Mar 2014"},
{labelDate: "Apr 2014", matterCount: "17"},
{labelDate: "May 2014", matterCount: "21"}];
desired outcome
merge = [{labelDate: "Jun 2013", matterCount: "1"},
{labelDate: "Jul 2013", matterCount: "2"},
{labelDate: "Aug 2013", matterCount: "2"},
{labelDate: "Sep 2013", matterCount: "10"},
{labelDate: "Oct 2013", matterCount: "1"},
{labelDate: "Nov 2013", matterCount: "1"},
{labelDate: "Dec 2013", matterCount: "0"},
{labelDate: "Jan 2014", matterCount: "0"},
{labelDate: "Feb 2014", matterCount: "1"},
{labelDate: "Mar 2014", matterCount: "0"},
{labelDate: "Apr 2014", matterCount: "17"},
{labelDate: "May 2014", matterCount: "21"}];