简体   繁体   English

在JavaScript中的对象嵌套数组中查找百分比值

[英]Find percentage value in a nested arrays of object in javascript

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" . 数据对象具有两个数组“ distinctCount”“ groupByAndCount” The distinctCount contains an object . distanceCount包含一个对象。

     {
        "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" 基于对象的键名称的“ groupByAndCount”应进行分组,并且应获取计数最高的值,然后将其除以“ distinctCount”中获得的值,然后乘以100以获取百分比。示例:在键“ marital_status”中“单个“值具有最高计数“ 6” ,而值“已婚”具有较低计数“ 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 我是reduce函数的初学者,我认为reducer函数是获取带有百分比的简化对象所必需的

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
        }
      ]
    ]
};

First your total count is unique so you can take it out in variable to make calculation easy. 首先,您的total count是唯一的,因此您可以将其取出以简化计算。 You can do that using a simple search on array of objects( data.distinctCount ). 您可以通过对对象数组( data.distinctCount )进行简单搜索来做到这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 ). 其次,您只希望每个数组( data.groupByAndCount内部的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 ; 现在,只需与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. 您绝对可以改善我的答案,使其更加优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM