我有3个阵列:
var countries = ['Montenegro', 'Spain', 'Belgium']
var years = ['2000', '2001', '2002', '2003'];
var death = ['cancer', 'measles'];
和一系列对象:
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'}
];
我想要的是一个完整的对象数组",它包含三个数组中存在的所有可能的值组合。
实际上, values
不包含与以下内容相关的对象:
{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', ...}
所以我想得到这个新对象(缺少的数据设置为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} // <--
];
新数组包含24个元素= countries.length * years.length * death.length
。
最好的方法是什么?
答案 0 :(得分:2)
您可以检查所有可能的组合。使用map
循环遍历每个数组并使用find
var countries = ['Montenegro', 'Spain', 'Belgium']
var years = ['2000', '2001', '2002', '2003'];
var death = ['cancer', 'measles'];
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'}
];
var valuesComplete = [countries, years, death].reduce((c, v) => [].concat(...c.map(o => v.map(x => [].concat(o, x)))))
.map(o => values.find(v => v.country === o[0] && v.year === o[1] && v.death === o[2]) || {country: o[0],year: o[1],death: o[2],number: NaN})
console.log(valuesComplete);
答案 1 :(得分:2)
这是我能想到的。
var countries = ['Montenegro', 'Spain', 'Belgium']
var years = ['2000', '2001', '2002', '2003'];
var death = ['cancer', 'measles'];
var valuesComplete = [];
function getDeathByYearCountry(country, year, disease) {
//your logic to build the number
return NaN;
}
countries.forEach(function(country) {
years.forEach(function(year) {
death.forEach(function(disease) {
valuesComplete.push({
country: country,
year: year,
death: disease,
number: getDeathByYearCountry(country,year,disease)
});
});
});
});
console.log(valuesComplete.length);
console.log(valuesComplete);