0

In a nested object of arrays , the percentage value should be mapped to an object which has greater count . The data object has two arrays "distinctCount" and "groupByAndCount" . The distinctCount contains an object .

     {
        "key": "total",
        "value": 7
      }

The "groupByAndCount" based on the key name the objects should be grouped and the value with highest count should be fetched and divided by value obtained in "distinctCount" and multiplied by 100 to get the percentage Example: In key "marital_status" the "Single" value has the highest count "6" compared to the value "Married" which has low count "1"

       {
          "key": "marital_status",
          "value": "Single",
          "count": 6/7*100 = 85.7%
        }

I am a beginner in reduce function , I think reducer function is required in getting the reduced object with percentage

const result = data.groupByAndCount.map(e => e.reduce((a, { key, value, count }) => 
(a['key'] = key,a['value'] = value,a['count'] = count, a), {}));
const data = {
    distinctCount : [
      {
        "key": "total",
        "value": 7
      },
      {
        "key": "member_total",
        "value": 7
      }
    ]
    ,
    groupByAndCount : [
      [
        {
          "key": "marital_status",
          "value": "Married",
          "count": 1
        },
        {
          "key": "marital_status",
          "value": "Single",
          "count": 6
        }
      ],
      [
        {
          "key": "payment",
          "value": "POST",
          "count": 7
        }
      ],
      [
        {
          "key": "customer_code",
          "value": "ABC",
          "count": 1
        },
        {
          "key": "customer_code",
          "value": "DEF",
          "count": 6
        }
      ]
    ]
};

The expected result :

const result = {
    distinctCount : [
      {
        "key": "total",
        "value": 7
      },
      {
        "key": "member_total",
        "value": 7
      }
    ]
    ,
    groupByAndCount : [
      [
        {
          "key": "marital_status",
          "value": "Single",
          "count": 85.71
        }
      ],
      [
        {
          "key": "payment",
          "value": "POST",
          "count": 100
        }
      ],
      [
        {
          "key": "customer_code",
          "value": "DEF",
          "count": 85.71
        }
      ]
    ]
};
3
  • How we can decide which distinctCount object belongs to which groupByAndCount object? As both have different key values and different count itself. Commented Sep 14, 2019 at 5:31
  • need to take only "total" key value which is "7" for all group objects Commented Sep 14, 2019 at 5:33
  • 1
    Okay, let me try, will let you know. Commented Sep 14, 2019 at 5:34

1 Answer 1

1

First your total count is unique so you can take it out in variable to make calculation easy. You can do that using a simple search on array of objects(data.distinctCount).

function search(array, value){
    for (var i=0; i < array.length; i++) {
        if (array[i].key === value) { 
            return array[i].value; 
        }
    }
}

const total_value = search(data.distinctCount, 'total');

Output:
7

Second, you want object with max count from each array only(array inside data.groupByAndCount).

const maxGroupByAndCount = data.groupByAndCount.map(function(arr){
                               return [arr.sort((a,b)=>b.count-a.count)[0]];
                           });

Output:
[
    [{key: "marital_status", value: "Single", count: 6}],
    [{key: "payment", value: "POST", count: 7}],
    [{key: "customer_code", value: "DEF", count: 6}]
]

Now, just convert each count in percentage by comparing with total_value;

maxGroupByAndCount.map(function(arr){
  arr[0].count = (arr[0].count/ total_value) * 100;
});

console.log(maxGroupByAndCount);

Output:
[
   [{key: "marital_status", value: "Single", count: 85.71428571428571}],
   [{key: "payment", value: "POST", count: 100}],
   [{key: "customer_code", value: "DEF", count: 85.71428571428571}]
]

Now you can do object assign to make it one.

const result = { 
                  'distinctCount' : data.distinctCount, 
                  'groupByAndCount': maxGroupByAndCount
               };

You can definitely improve my answer to make it more optimized.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.