• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Javascript Avg functions

Explorer ,
Mar 10, 2021 Mar 10, 2021

Copy link to clipboard

Copied

Hi All 

I have a data in a text File i am reading that file and i wanted to find Avg of all male and female Annual income with respect to country 

The output is like if i wanted to find Any country avg of male and femal employee

The data is: 

City,Country,Currencycode,Gender,AnnualIncome
Delhi,India,INR,M,15000
Mumbai,India,INR,M,20000
Kolkata,India,INR,M,10000
Bangalore,India,INR,F,25000
Paris,France,EUR,M,8000
Nice,France,EUR,F,12000
Toulouse,France,EUR,M,5000
Marsellie,France,EUR,F,6000
London,United Kingdom,GBP,M,40000

 

thanks in Adavance

Anupam

TOPICS
Bug , Scripting

Views

99

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2021 Mar 10, 2021

Copy link to clipboard

Copied

LATEST

Seems like a job for Excel. But if that is out of the question, I'd probably just use custom objects as I read each line. Each core object as a country, male and female for each, and counters and tallies for the sum. 

for line: 

coreObj[line[1][line[3]].salary += parseInt(line[4]);

coreObj[line[1]][line[3]].count++;

 

Of course you'd have to instantiate each object and subobject first. I typically use if (obj.hasOwnProperty). So like: 

if (!coreObj.hasOwnProperty(country)) {

      coreObj[country]["M"] = {

            salary: 0,

            count: 0

      }

}

 

Do the same for F. Once you have total salary and count for each gender for each country, the calculation is easy. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines