Vithor Carlos
Vithor Carlos

Reputation: 141

I can't bring the data to the app the way facebook sends it

enter image description here

I need to fetch the 3 ages with the most followers of this object. Not only the age range, but I also need to add up the chosen age of all genders. For example: If 18-24 was the result with the highest amount, then I need to add: Male(100 Followers) + Female(120 Followers) + Other(10 Followers) = 230 Followers ... You have to show the age range on the screen and I need to know the total amount of the age range and all the data to make the progress bar and convert to percentage... I've tried everything and I couldn't

Object:

`

"value": {
          "U.45-54": 10,
          "U.35-44": 14,
          "U.25-34": 16,
          "U.55-64": 1,
          "U.13-17": 1,
          "M.65+": 3,
          "U.18-24": 8,
          "M.55-64": 8,
          "M.45-54": 27,
          "F.35-44": 287,
          "M.35-44": 102,
          "F.55-64": 37,
          "M.18-24": 17,
          "M.13-17": 1,
          "F.65+": 5,
          "F.25-34": 335,
          "F.45-54": 119,
          "M.25-34": 111,
          "F.18-24": 58
        }

`

I tried everything and I couldn't. I need to bring the data as it is in the image

enter image description here

Upvotes: 5

Views: 55

Answers (1)

Avetik Nersisyan
Avetik Nersisyan

Reputation: 354

I think you want this.

    const value ={
        "U.45-54": 10,
        "U.35-44": 14,
        "U.25-34": 16,
        "U.55-64": 1,
        "U.13-17": 1,
        "M.65+": 3,
        "U.18-24": 8,
        "M.55-64": 8,
        "M.45-54": 27,
        "F.35-44": 287,
        "M.35-44": 102,
        "F.55-64": 37,
        "M.18-24": 17,
        "M.13-17": 1,
        "F.65+": 5,
        "F.25-34": 335,
        "F.45-54": 119,
        "M.25-34": 111,
        "F.18-24": 58
      }


      const arr = Object.keys(value);

      const res = arr.reduce((accum, item) => {
            const isFemale = item.startsWith('F')
            const isMale = item.startsWith('M')
            const ageRange = item.split('.')[1]
            if( isFemale) {
                if(!accum.Female[ageRange]) {
                    accum['Female'] = {
                        ...accum.Female,
                        [ageRange]: value[item]
                    }
                } else {
                    accum['Female'] = {
                        [ageRange]: accum.Female[ageRange] + value[item]
                    }
                }
                
            }  else if(isMale) {
                if(!accum.Male[ageRange]) {
                    accum['Male'] = {
                        ...accum.Male,
                        [ageRange]: value[item]
                    }
                } else {
                    accum['Male'] = {
                        [ageRange]: accum.Male[ageRange] + value[item]
                    }
                }
            } else {
                   if(!accum.Undefined[ageRange]) {
                    accum['Undefined'] = {
                        ...accum.Undefined,
                        [ageRange]: value[item]
                    }
                } else {
                    accum['Undefined'] = {
                        [ageRange]: accum.Undefined[ageRange] + value[item]
                    }
                }
            }

            return accum;
      }, {
        Female: {},
        Male : {},
        Undefined: {},
      })


      console.log(res);

Upvotes: 2

Related Questions