Вы можете просто инициализировать array
двумя objects
, первый для Male
, а второй для Female
.
let result = [{text: "Male", total:0}, {text: "Female", total:0}];
Затем заполнить его соответствующими данными с помощьюforEach
цикл над вашим первым array
.
array.forEach(function(a){
if(a.genderAge.indexOf("Male")>-1){
result[0].total += a.count;
}else if(a.genderAge.indexOf("Female")>-1){
result[1].total += a.count;
}
});
Демо:
var array = [
{genderAge: "Male 25-39", count: 13029, weightedCount: 18475.6824262314, matchingSegment: 2},
{genderAge: "Female 55+", count: 35639, weightedCount: 32294.5926147014, matchingSegment: 8},
{genderAge: "Female 25-39", count: 23285, weightedCount: 20645.0599977815, matchingSegment: 6},
{genderAge: "Female 18-24", count: 7745, weightedCount: 8497.30029399032, matchingSegment: 5},
{genderAge: "Male 55+", count: 38018, weightedCount: 28589.2793936886, matchingSegment: 4},
{genderAge: "Male 18-24", count: 4038, weightedCount: 8122.65161312996, matchingSegment: 1},
{genderAge: "Female 40-54", count: 23051, weightedCount: 22834.3167597392, matchingSegment: 7},
{genderAge: "Male 40-54", count: 17278, weightedCount: 19681.8852663563, matchingSegment: 3}
];
let result = [{text: "Male", total:0}, {text: "Female", total:0}];
array.forEach(function(a){
if(a.genderAge.indexOf("Male")>-1){
result[0].total += a.count;
}else if(a.genderAge.indexOf("Female")>-1){
result[1].total += a.count;
}
});
console.log(result);