Home > other >  How to loop through an array of Objects, Keep count of one of the Object's value and then retur
How to loop through an array of Objects, Keep count of one of the Object's value and then retur

Time:06-20

I have been trying to get the top n languages in an array of country objects and return the top n languages as an object of the language and it's count value. I tried using two loops, one nested in the other but I got stuck. Here is a short example of the array of country Objects...

const countries = [
{
 name: 'Afghanistan',
 capital: 'Kabul',
 languages: ['Pashto', 'Uzbek', 'Turkmen'],
 population: 27657145,
 flag: 'https://restcountries.eu/data/afg.svg',
 currency: 'Afghan afghani'
},
{
 name: 'Åland Islands',
 capital: 'Mariehamn',
 languages: ['Swedish'],
 population: 28875,
 flag: 'https://restcountries.eu/data/ala.svg',
 currency: 'Euro'
},
{
 name: 'Albania',
 capital: 'Tirana',
 languages: ['Albanian'],
 population: 2886026,
 flag: 'https://restcountries.eu/data/alb.svg',
 currency: 'Albanian lek'
},
{
 name: 'Algeria',
 capital: 'Algiers',
 languages: ['Arabic'],
 population: 40400000,
 flag: 'https://restcountries.eu/data/dza.svg',
 currency: 'Algerian dinar'
},
{
 name: 'American Samoa',
 capital: 'Pago Pago',
 languages: ['English', 'Samoan'],
 population: 57100,
 flag: 'https://restcountries.eu/data/asm.svg',
 currency: 'United State Dollar'
},
{
 name: 'Andorra',
 capital: 'Andorra la Vella',
 languages: ['Catalan'],
 population: 78014,
 flag: 'https://restcountries.eu/data/and.svg',
 currency: 'Euro'
},
{
 name: 'Angola',
 capital: 'Luanda',
 languages: ['Portuguese'],
 population: 25868000,
 flag: 'https://restcountries.eu/data/ago.svg',
 currency: 'Angolan kwanza'
},
{
 name: 'Anguilla',
 capital: 'The Valley',
 languages: ['English'],
 population: 13452,
 flag: 'https://restcountries.eu/data/aia.svg',
 currency: 'East Caribbean dollar'
},const countries = [
{
 name: 'Afghanistan',
 capital: 'Kabul',
 languages: ['Pashto', 'Uzbek', 'Turkmen'],
 population: 27657145,
 flag: 'https://restcountries.eu/data/afg.svg',
 currency: 'Afghan afghani'
},
{
 name: 'Åland Islands',
 capital: 'Mariehamn',
 languages: ['Swedish'],
 population: 28875,
 flag: 'https://restcountries.eu/data/ala.svg',
 currency: 'Euro'
},
{
 name: 'Albania',
 capital: 'Tirana',
 languages: ['Albanian'],
 population: 2886026,
 flag: 'https://restcountries.eu/data/alb.svg',
 currency: 'Albanian lek'
},
{
 name: 'Algeria',
 capital: 'Algiers',
 languages: ['Arabic'],
 population: 40400000,
 flag: 'https://restcountries.eu/data/dza.svg',
 currency: 'Algerian dinar'
},
{
 name: 'American Samoa',
 capital: 'Pago Pago',
 languages: ['English', 'Samoan'],
 population: 57100,
 flag: 'https://restcountries.eu/data/asm.svg',
 currency: 'United State Dollar'
},
{
 name: 'Andorra',
 capital: 'Andorra la Vella',
 languages: ['Catalan'],
 population: 78014,
 flag: 'https://restcountries.eu/data/and.svg',
 currency: 'Euro'
},
{
 name: 'Angola',
 capital: 'Luanda',
 languages: ['Portuguese'],
 population: 25868000,
 flag: 'https://restcountries.eu/data/ago.svg',
 currency: 'Angolan kwanza'
},
{
 name: 'Anguilla',
 capital: 'The Valley',
 languages: ['English'],
 population: 13452,
 flag: 'https://restcountries.eu/data/aia.svg',
 currency: 'East Caribbean dollar'
}]

And this is what I'm expecting to get


// My output should look like this
console.log(mostSpokenLanguages(countries, 5))
/*[
{country: 'English',count:91},
{country: 'French',count:45},
{country: 'Arabic',count:25},
{country: 'Spanish',count:24},
{country:'Russian',count:9}
]*/

This is what I tried


const mostSpokenLanguage = (arr, count) => {
let languageCountObjArr = [];
let languagesArr = arr.map(countryObj => countryObj.languages);
console.log('Languages Array', languagesArr);
languagesArr.forEach(languageArr => {
 for (let i = 0; i < languageArr.length; i  ) {
   /* I GOT STUCK HERE
     let langObj = {
     country: languageArr[i],
     count: 1
   };
   if (languageCountObjArr[i].country === languageArr[i]) {
     let
   }*/
   languageCountObjArr.push(langObj);
 }
});
languageCountObjArr = languageCountObjArr.slice(count);
return languageCountObjArr;
};

I just started JavaScript a couple of weeks now and this stuff has been giving me worries. Please I would be very happy if I can get the answer to this question.

CodePudding user response:

You can do:

const countries = [{name: 'Afghanistan',capital: 'Kabul',languages: ['Pashto', 'Uzbek', 'Turkmen'],population: 27657145,flag: 'https://restcountries.eu/data/afg.svg',currency: 'Afghan afghani',},{name: 'Åland Islands',capital: 'Mariehamn',languages: ['Swedish'],population: 28875,flag: 'https://restcountries.eu/data/ala.svg',currency: 'Euro',},{name: 'Albania',capital: 'Tirana',languages: ['Albanian'],population: 2886026,flag: 'https://restcountries.eu/data/alb.svg',currency: 'Albanian lek',},{name: 'Algeria',capital: 'Algiers',languages: ['Arabic'],population: 40400000,flag: 'https://restcountries.eu/data/dza.svg',currency: 'Algerian dinar',},{name: 'American Samoa',capital: 'Pago Pago',languages: ['English', 'Samoan'],population: 57100,flag: 'https://restcountries.eu/data/asm.svg',currency: 'United State Dollar',},{name: 'Andorra',capital: 'Andorra la Vella',languages: ['Catalan'],population: 78014,flag: 'https://restcountries.eu/data/and.svg',currency: 'Euro',},{name: 'Angola',capital: 'Luanda',languages: ['Portuguese'],population: 25868000,flag: 'https://restcountries.eu/data/ago.svg',currency: 'Angolan kwanza',},{name: 'Anguilla',capital: 'The Valley',languages: ['English'],population: 13452,flag: 'https://restcountries.eu/data/aia.svg',currency: 'East Caribbean dollar',},]

const countriesHash = countries.reduce((a, { languages }) =>
  (languages.forEach((l) => (a[l] = (a[l] || 0)   1)), a), {})

const result = Object.entries(countriesHash)
  .map(([country, count]) => ({ country, count }))
  .sort((a, b) => b.count - a.count)

console.log(result)

CodePudding user response:

A more dynamic approach to use based on array of obj and its property and get the count based on its number of occurences.

const countries = [
{
 name: 'Afghanistan',
 capital: 'Kabul',
 languages: ['Pashto', 'Uzbek', 'Turkmen']
},
{
 name: 'Åland Islands',
 capital: 'Mariehamn',
 languages: ['Swedish']
},
{
 name: 'Albania',
 capital: 'Tirana',
 languages: ['Albanian']
},
{
 name: 'Algeria',
 capital: 'Algiers',
 languages: ['Arabic']
},
{
 name: 'American Samoa',
 capital: 'Pago Pago',
 languages: ['English', 'Samoan']
},
{
 name: 'Andorra',
 capital: 'Andorra la Vella',
 languages: ['Catalan']
},
{
 name: 'Angola',
 capital: 'Luanda',
 languages: ['Portuguese']
},
{
 name: 'Anguilla',
 capital: 'The Valley',
 languages: ['English']
},
{
 name: 'Afghanistan',
 capital: 'Kabul',
 languages: ['Pashto', 'Uzbek', 'Turkmen']
},
{
 name: 'Åland Islands',
 capital: 'Mariehamn',
 languages: ['Swedish']
},
{
 name: 'Albania',
 capital: 'Tirana',
 languages: ['Albanian']
},
{
 name: 'Algeria',
 capital: 'Algiers',
 languages: ['Arabic']
},
{
 name: 'American Samoa',
 capital: 'Pago Pago',
 languages: ['English', 'Samoan']
},
{
 name: 'Andorra',
 capital: 'Andorra la Vella',
 languages: ['Catalan']
},
{
 name: 'Angola',
 capital: 'Luanda',
 languages: ['Portuguese']
},
{
 name: 'Anguilla',
 capital: 'The Valley',
 languages: ['English']
}]

function groupBy(objectArray, property) { 
    return objectArray.reduce(function (acc, obj) {
     let key = obj[property]
     if (!acc[key]) {
      acc[key] = {}
      acc[key][property] = key
      acc[key].count = 0
     }
     acc[key].count  
     return acc 
     }, {})
    }
    
const result = groupBy(countries, 'name')
console.log(result)

  • Related