I have 2 arrays as below
var dataSource = [
{location: "France", January: 79, February: 81, March: 23},
{location: "Germany", January: 98, February: 83},
{location: "Japan", January: 96, March: 11} ];
var Months = ["January","February","March"];
I want to loop through each object in dataSource and check if all values of Months exist in each objects of dataSource. If the value doesn't exist in dataSource then add that value to dataSource with value = 100
example : in location Germany, the month "March" does not exist so I need to push the key and value March : 100
at the end dataSource should be as below
var dataSource = [
{location: "France", January: 79, February: 81, March: 23},
{location: "Germany", January: 98, February: 83, March: 100},
{location: "Japan", January: 96, February: 100, March: 11} ];
I tried many solutions from previous threads but I am not getting the exact result I want. Here are some of my ideas
var dataSource = [
{location: "France", January: 79, February: 81, March: 23},
{location: "Germany", January: 98, February: 83},
{location: "Japan", January: 96, March: 11} ];
var Months = ["January","February","March"];
dataSource.forEach(function(element) {
Months.forEach(function(item) {
if (!(item in element)) {
//Object.assign(dataSource, {item: 100});
//dataSource = {...dataSource, ...{item: 100}}
dataSource.push({item: 100});
}
});
});
console.log(dataSource);
Thank you for your suggestions.