计算对象的百分比

calculate ther percentage of object

我的函数映射一个对象来创建新闻键。我的新键 "percent" 计算键 "data" 每个值的百分比。 百分比 = 所有值的 value/sum。 我不知道为什么它不起作用?

 

var myObject = {
  id1: {
   "value 1": 3,
   "value 2": 2
  },
  id2: {
   "value 1": 2,
   "value 2": 2
  },
  id3: {
   "value 1": 4,
   "value 2": 3
  }
};
 
 var series = _(myObject).map(function(g, key) {
 
var total = 0;
  for (var i in Object.values(g)) {
   total += Object.values(g)[i];
  }
 
  return {
   type: 'column',
   name: key,
   total: total,
   data: Object.values(g),
   percent:Object.values(g)/total
   };
});
 console.log(series)

预期的结果

[{
 "type": "column",
 "name": "id1",
 "total": 5,
 "data": [ 3, 2 ],
 "percent": [ 0.6, 0.4 ],
}, {
 "type": "column",
 "name": "id2",
 "total": 4, 
"data": [ 2, 2 ],
 "percent": [ 0.5, 0.5 ],
}, {
 "type": "column",
 "name": "id3",
 "total": 7,
 "data": [ 4, 3 ],
 "percent": [ 0.57, 0.43 ],
}]

您可以通过获取 key/value 对来映射新对象,首先获取值的总和,然后计算所需的百分比(不是基于 100,而是基于 1)。

var object = { id1: { "value 1": 3, "value 2": 2 }, id2: { "value 1": 2, "value 2": 2 }, id3: { "value 1": 4, "value 2": 3 } },
    result = Object.entries(object).map(([name, values]) => {
        var data = Object.values(values),
            total = data.reduce((a, b) => a + b, 0),                
            percent = data.map(p => p / total);

        return { type: 'column', name, total, data, percent };
    });
 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

适用于 ES5 语法的 IE。

var object = { id1: { "value 1": 3, "value 2": 2 }, id2: { "value 1": 2, "value 2": 2 }, id3: { "value 1": 4, "value 2": 3 } },
    result = Object.keys(object).map(function(name) {
        var data = Object.keys(object[name]).map(function (k) { return object[name][k]; }),
            total = data.reduce(function (a, b) { return a + b; }, 0),                
            percent = data.map(function (p) { return p / total; });

        return { type: 'column', name: name, total: total, data: data, percent: percent };
    });
 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

您可以使用 Object.keys() and Object.values() 来获得所需的结果。

演示版

ES6

var myObject = {id1: {"value 1": 3,"value 2": 2},id2: {"value 1": 2,"value 2": 2},id3: {"value 1": 4,"value 2": 3}};

let result = Object.keys(myObject).map(v => {
  let data = Object.values(myObject.id1),
    sum = data.reduce((r, x) => r + x);

  return {
    type: 'column',
    name: v,
    total: sum,
    data: data,
    percent: data.map(p => p / sum)
  }
});

console.log(result);
.as-console-wrapper {max-height: 100% !important;top: 0;}

ES5

var myObject = {id1: {"value 1": 3,"value 2": 2},id2: {"value 1": 2,"value 2": 2},id3: {"value 1": 4,"value 2": 3}};

let result = Object.keys(myObject).map(function(v) {
  let data = Object.values(myObject.id1),
    sum = data.reduce(function(r, x) {return r + x});

  return {
    type: 'column',
    name: v,
    total: sum,
    data: data,
    percent: data.map(function(p){return  p / sum})
  }
});

console.log(result);
.as-console-wrapper {max-height: 100% !important;top: 0;}