簡體   English   中英

如何獲取JavaScript對象數組中的總值

[英]How to get total of values in array of javascript objects

我有以下數組:

[
{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}
]

我想將此對象數組轉換為包含兩個對象的數組,其中兩個對象包含男性和女性的匯總數據(count,weightedCount和matchingSegment)。 是否有使用Lodash和/或ES6實現此目的的簡便方法?

更新:抱歉,我提供了錯誤的數據。

function sumFor(pattern, data) {
  return data
    .filter(item => item.text.match(pattern))
    .reduce((accu, item) => accu + item.total, 0);
}
sumFor(/^Male/, data); // 125480
sumFor(/^Female/, data); // 154916

您可以按期望值分組並獲得total

 var data = array = [{ text: "Male 18-24 (12886)", value: "test_1", total: 12886 }, { text: "Male 25-39 (27913)", value: "test_2", total: 27913 }, { text: "Male 40-54 (29793)", value: "test__3", total: 29793 }, { text: "Male 55+ (54888)", value: "test__4", total: 54888 }, { text: "Female 18-24 (19354)", value: "test_5", total: 19354 }, { text: "Female 25-39 (43972)", value: "test_6", total: 43972 }, { text: "Female 40-54 (39327)", value: "test_7", total: 39327 }, { text: "Female 55+ (52263)", value: "test_8", total: 52263 }], result = _(data) .groupBy(o => o.text.match(/^\\S+/)[0]) .map((array, group) => ({ group, sum: _.sumBy(array, 'total') })) .value(); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script> 

 const { Sum, mreduceMap, compose, propOr, ifElse, constant } = crocks const data = [ {text: "Male 18-24 (12886)", value: "test_1", total: 12886}, {text: "Male 25-39 (27913)", value: "test_2", total: 27913}, {text: "Male 40-54 (29793)", value: "test__3", total: 29793}, {text: "Male 55+ (54888)", value: "test__4", total: 54888}, {text: "Female 18-24 (19354)", value: "test_5", total: 19354}, {text: "Female 25-39 (43972)", value: "test_6", total: 43972}, {text: "Female 40-54 (39327)", value: "test_7", total: 39327}, {text: "Female 55+ (52263)", value: "test_8", total: 52263} ] const startsWith = x => str => typeof str === 'string' && str.startsWith(x) const isSex = sex => compose(startsWith(sex), propOr('', 'text')) const totalFor = sex => ifElse(isSex(sex), propOr(0, 'total'), constant(0)) const sumSex = sex => mreduceMap(Sum, totalFor(sex)) const sumMales = sumSex('Male') const sumFemales = sumSex('Female') const males = sumMales(data) const females = sumFemales(data) console.log({males, females}) 
 <script src="https://unpkg.com/crocks@0.10.1/dist/crocks.min.js"></script> 

使用crocks庫,您可以使用功能強大的方法來解決問題。

您可以使用減少方法

var list = [
{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}
]

list.reduce(function(a, b) {
     if(/^Male/.test(b['genderAge'])) {
        a['male']['pop'] += b['count'];
        a['male']['matchingSegment'] += b['matchingSegment'];
        a['male']['weightedCount'] += b['weightedCount'];
    } else if(/^Female/.test(b['genderAge'])){
        a['female']['pop'] += b['count'];
        a['female']['matchingSegment'] += b['matchingSegment'];
        a['female']['weightedCount'] += b['weightedCount'];
    }
     return a;
}, {'male':{'pop':0, 'matchingSegment':0, 'weightedCount':0}, 'female':{'pop':0, 'matchingSegment':0, 'weightedCount':0}});

ES6

.reduce((acc,e)=>{
  if (/^Male/.test(e.text)){
   acc[0].total+=e.total
  }
  if (/^Female/.test(e.text)){
   acc[1].total+=e.total
  }
  return acc
},[{total:0},{total:0}])

您可以簡單地用兩個objects初始化array ,第一個objects用於Male ,第二個對象用於Female

let result = [{text: "Male", total:0}, {text: "Female", total:0}];

然后在第一個array上使用forEach循環用相關數據填充它。

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); 

這是另一種簡單的lodash方法,它使用_.groupBy並且僅執行一個循環:

 var data = [ {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} ] const getGenderTotals = data => { let Males = 0, Females = 0 _.groupBy(data, (r => {/^Male/.test(r.genderAge) ? Males += r.count : Females += r.count})) return { Males, Females } } console.log(getGenderTotals(data)) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script> 

由於我們可以訪問該值並且位於_.groupBy上下文中,因此您也可以輕松地在groupBy iteratee函數內返回MaleFemale以獲得實際的分組等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM