0

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?

  • 1
    Please edit your question to include your attempts. If you haven't made any yet, consider doing some research and taking a stab at it yourself before asking for help - we're here to *debug* your code, not write it from scratch! [This seems like a good place to start](https://stackoverflow.com/questions/4331092/finding-all-combinations-of-javascript-array-values). – Tyler Roper Jun 02 '18 at 05:09
  • @Durga Numeric data is just a stupid example to make the structure understand. –  Jun 02 '18 at 05:11

2 Answers2

1

You can check all possible combinations. Use map to loop thru each array and search it using 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);
Eddie
  • 26,593
  • 6
  • 36
  • 58
  • This actually isn't [permutation](https://en.wikipedia.org/wiki/Permutation), but rather [*combination*](https://en.wikipedia.org/wiki/Combination). In any event, good answer :) – Tyler Roper Jun 02 '18 at 05:14
  • 1
    Thanks for the clarification. I guess we learn something new everyday :) – Eddie Jun 02 '18 at 05:24
1

This is what I can think of.

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);
Karthikeyan
  • 302
  • 1
  • 11