My task involves dealing with an array of months (var months
). I want to filter out the months that are missing in another array of objects (var array1
) and add a property Avg
with a value of 0 for those missing months.
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var array1 = [{Month: 'Jan', Avg: 10},{Month: 'Feb', Avg: 20},
{Month: 'May', Avg: 50},{Month: 'Jun', Avg: 60}];`
For example, if the array1
has entries for Jan
, Feb
, May
, and Jun
, I aim to identify the missing months and assign a value of 0 to the Avg
property. This means adding entries like
{Month: 'Mar', Avg: 0}, {Month: 'Apr', Avg: 0}, ...
for the absent months when compared with the months
array.
The expected updated version of array1
should look like this:
[{Month: 'Jan', Avg: 10}, {Month: 'Feb', Avg: 20},
{Month: 'Mar', Avg: 0}, {Month: 'Apr', Avg: 0},
{Month: 'May', Avg: 50}, {Month: 'Jun', Avg: 60},
{Month: 'Jul', Avg: 0}, {Month: 'Aug', Avg: 0},
{Month: 'Sep', Avg: 0}, {Month: 'Oct', Avg: 0},
{Month: 'Nov', Avg: 0}, {Month: 'Dec', Avg: 0}];