Skip to content

[Need-Help]: How to return an array of objects from reduce method? #687

@alamenai

Description

@alamenai

Question : Find the 10 most spoken languages:

const mostSpokenLanguage = (countries, limit) =>
  countries
    .map((country) => country.languages)
    .flat()
    .reduce((acc, language) => {
      return acc[language] ? ++acc[language] : (acc[language] = 1), acc;
    }, []);

console.log(mostSpokenLanguage(countriesList, 10));

Current :

 Uzbek: 2,
  Turkmen: 2,
  Swedish: 3,
  Albanian: 3,
  Arabic: 25,
  English: 91,

Expected:

{country: Uzbek, count:2}
{.....},
{.....}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions