[{ FirstName: 'Pavel', LastName: 'Pavlol', Phone: '1234567', Gender:'male' },
{ FirstName: 'Ivan', LastName: 'Ivanov', Phone: '1234567', Gender:'female' }]
[{"label":"Male", "value":50},
{"label":"Female", "value":50}];
var arr = [{ FirstName: 'Pavel', LastName: 'Pavlol', Phone: '1234567', Gender:'male' },
{ FirstName: 'Ivan', LastName: 'Ivanov', Phone: '1234567', Gender:'female' }];
var males = arr.filter((item)=>(item.Gender == 'male' ));
var females = arr.filter((item)=>(item.Gender == 'female'));
var result = [{
"label" : "male",
"value" : (males.length*100)/arr.length
},
{
"label" : "female",
"value" : (females.length*100)/arr.length
}]
function countFieldPersent(arr, field) {
var data = arr.reduce((r, item) => {
var value = String(item[field]);
if(typeof r[value] === 'undefined') {
r[value] = 0;
}
return ++r[value], r;
}, {});
return Object.getOwnPropertyNames(data).map(label => ({
label,
value : (data[label] * 100) / arr.length
}));
}
var arr = [{ FirstName: 'Pavel', LastName: 'Pavlol', Phone: '1234567', Gender:'male' },
{ FirstName: 'Ivan', LastName: 'Ivanov', Phone: '1234567', Gender:'female' }];
var result = countFieldPersent(arr, 'Gender');
Правда в моем варианте значения будут сравниваться как строки, так как я их перевожу в ключи объекта data var arr = [{ FirstName: 'Pavel', LastName: 'Pavlol', Phone: '1234567', Gender:'male' },
{ FirstName: 'Ivan', LastName: 'Ivanov', Phone: '1234567', Gender:'female' }];
var count = 0;
for (i=0;i<arr.length;i++){
if (arr[i].Gender == 'male'){
count++;
}
}
var percentMale = Math.round(count / arr.length * 100);
var percentFemale = 100 - percentMale;
var arr2 = [{"label":"Male", "value":percentMale},
{"label":"Female", "value":percentFemale}];