create an array of all countries in the world with columns of the name of that country in the most common 20 languages in javascript

index.tsx
const countriesInLanguages = [
  { country: 'Afghanistan', languages: ['Pashto', 'Dari'] },
  { country: 'Albania', languages: ['Albanian'] },
  { country: 'Algeria', languages: ['Arabic'] },
  { country: 'Andorra', languages: ['Catalan'] },
  { country: 'Angola', languages: ['Portuguese'] },
  { country: 'Antigua and Barbuda', languages: ['English'] },
  { country: 'Argentina', languages: ['Spanish'] },
  { country: 'Armenia', languages: ['Armenian'] },
  { country: 'Australia', languages: ['English'] },
  { country: 'Austria', languages: ['German'] },
  { country: 'Azerbaijan', languages: ['Azerbaijani'] },
  // Add more countries and their languages here
];

const topLanguages = ['English', 'Chinese', 'Spanish', 'Arabic', 'French', 'Russian', 'Portuguese', 'German', 'Italian', 'Dutch', 'Japanese', 'Korean', 'Turkish', 'Polish', 'Swedish', 'Norwegian', 'Danish', 'Finnish', 'Romanian', 'Czech'];

const countries = [];

for (let i = 0; i < countriesInLanguages.length; i++) {
  const country = countriesInLanguages[i].country;
  const languages = countriesInLanguages[i].languages.reduce((obj, lang) => {
    obj[lang] = topLanguages.slice(0, 20);
    return obj;
  }, {});
  countries.push({ country, languages });
}

console.log(countries);
1257 chars
30 lines

This code creates an array of all countries in the world, along with the name of that country in the most common 20 languages. It does this by starting with an array of objects that include each country and its primary language(s). It then loops through that array and creates a new object for each country that includes the country name and an object with the most common 20 languages as keys and an empty array as values. It does this by using the reduce() method on the array of languages and returning a new object with the 20 most common languages as keys and empty arrays as values. Finally, it pushes the new country object to an array of countries and logs it to the console.

Note: This is not an exhaustive list of all languages spoken in each country, nor does it take into account dialects or regional variations within languages.

gistlibby LogSnag