I have 3 arrays:
var countries = ['Montenegro', 'Spain', 'Belgium']
var years = ['2000', '2001', '2002', '2003'];
var death = ['cancer', 'measles'];
and an array of objects:
var values = [
{country:'Montenegro', year:'2000', death:'cancer', number:'50'},
{country:'Montenegro', year:'2001', death:'cancer', number:'60'},
{country:'Montenegro', year:'2002', death:'cancer', number:'70'},
{country:'Montenegro', year:'2003', death:'cancer', number:'80'},
{country:'Spain', year:'2000', death:'cancer', number:'NaN'},
{country:'Spain', year:'2001', death:'cancer', number:'110'},
{country:'Spain', year:'2002', death:'cancer', number:'120'},
{country:'Spain', year:'2003', death:'cancer', number:'130'},
{country:'Belgium', year:'2000', death:'cancer', number:'70'},
{country:'Belgium', year:'2001', death:'cancer', number:'80'},
{country:'Belgium', year:'2002', death:'cancer', number:'90.5'},
{country:'Montenegro', year:'2000', death:'measles', number:''},
{country:'Montenegro', year:'2001', death:'measles', number:'1'},
{country:'Montenegro', year:'2002', death:'measles', number:'2'},
{country:'Montenegro', year:'2003', death:'measles', number:'3'},
{country:'Spain', year:'2000', death:'measles', number:'2'},
{country:'Spain', year:'2001', death:'measles', number:'3'},
{country:'Spain', year:'2002', death:'measles', number:'NaN'},
{country:'Spain', year:'2003', death:'measles', number:'5'}
];
What I would like is an array of objects that is "complete", that contains all the possible combinations of values present in the three arrays.
values
, infact, doesn't contain objects related to:
{country:'Belgium', year:'2003', death:'cancer', ...},
{country:'Belgium', year:'2000', death:'measles', ...},
{country:'Belgium', year:'2001', death:'measles', ...},
{country:'Belgium', year:'2002', death:'measles', ...},
{country:'Belgium', year:'2003', death:'measles', ...}
So I would like to get this new object (the missing data is set to NaN
):
var valuesComplete = [
{country:'Montenegro', year:'2000', death:'cancer', number:'50'},
{country:'Montenegro', year:'2001', death:'cancer', number:'60'},
{country:'Montenegro', year:'2002', death:'cancer', number:'70'},
{country:'Montenegro', year:'2003', death:'cancer', number:'80'},
{country:'Spain', year:'2000', death:'cancer', number:'NaN'},
{country:'Spain', year:'2001', death:'cancer', number:'110'},
{country:'Spain', year:'2002', death:'cancer', number:'120'},
{country:'Spain', year:'2003', death:'cancer', number:'130'},
{country:'Belgium', year:'2000', death:'cancer', number:'70'},
{country:'Belgium', year:'2001', death:'cancer', number:'80'},
{country:'Belgium', year:'2002', death:'cancer', number:'90.5'},
{country:'Belgium', year:'2003', death:'cancer', number:NaN}, // <--
{country:'Montenegro', year:'2000', death:'measles', number:''},
{country:'Montenegro', year:'2001', death:'measles', number:'1'},
{country:'Montenegro', year:'2002', death:'measles', number:'2'},
{country:'Montenegro', year:'2003', death:'measles', number:'3'},
{country:'Spain', year:'2000', death:'measles', number:'2'},
{country:'Spain', year:'2001', death:'measles', number:'3'},
{country:'Spain', year:'2002', death:'measles', number:'NaN'},
{country:'Spain', year:'2003', death:'measles', number:'5'},
{country:'Belgium', year:'2000', death:'measles', number:NaN}, // <--
{country:'Belgium', year:'2001', death:'measles', number:NaN}, // <--
{country:'Belgium', year:'2002', death:'measles', number:NaN}, // <--
{country:'Belgium', year:'2003', death:'measles', number:NaN} // <--
];
The new array contains 24 elements = countries.length * years.length * death.length
.
What is the best way to do it?