使用 vanilla JavaScript 或其他 JavaScript 库(例如 lodash)聚合数据

问题描述 投票:0回答:1

你能帮我解决以下场景吗?

我想构建一个堆积条形图,显示拉丁美洲每年的凶杀百分比。将显示凶杀率较高的国家,而其余国家将显示为其他国家,并将各自的凶杀率相加。

这是一个例子。

假设我的数据数组是:

const data = [ 
  { year: '2020', country: 'Brazil', homicides: 0.60 },
  { year: '2020', country: 'Argentina', homicides: 0.10 }, 
  { year: '2020', country: 'Venezuela', homicides: 0.09 }, 
  { year: '2020', country: 'Uruguay', homicides: 0.08 }, 
  { year: '2020', country: 'Paraguay', homicides: 0.04 }, 
  { year: '2020', country: 'Bolivia', homicides: 0.03 }, 
  { year: '2020', country: 'Peru', homicides: 0.02 }, 
  { year: '2020', country: 'Chile', homicides: 0.02 }. 
  { year: '2020', country: 'Equador', homicides: 0.02 }, 
  { year: '2021', country: 'Brazil', homicides: 0.70 }, 
  { year: '2021', country: 'Venezuela', homicides: 0.10 }, 
  { year: '2021', country: 'Ecuador', homicides: 0.08 }, 
  { year: '2021', country: 'Argentina', homicides: 0.06 }, 
  { year: '2021', country: 'Paraguay', homicides: 0.02 }, 
  { year: '2021', country: 'Uruguay', homicides: 0.02 }, 
  { year: '2021', country: 'Chile', homicides: 0.01 },   
  { year: '2021', country: 'Peru', homicides: 0.01 }
];

如果

maxCategories = 6
,结果数组应该是:

const aggregatedData = [
  { year: '2020', country: 'Brazil', homicides: 0.60 },
  { year: '2020', country: 'Argentina', homicides: 0.10 },
  { year: '2020', country: 'Venezuela', homicides: 0.09 },
  { year: '2020', country: 'Uruguay', homicides: 0.08 },
  { year: '2020', country: 'Paraguay', homicides: 0.04 },
  { year: '2020', country: 'others', homicides: 0.09 },
  { year: '2021', country: 'Brazil', homicides: 0.70 },
  { year: '2021', country: 'Venezuela', homicides: 0.10 },
  { year: '2021', country: 'Ecuador', homicides: 0.08 },
  { year: '2021', country: 'Argentina', homicides: 0.06 },
  { year: '2021', country: 'Paraguay', homicides: 0.02 },
  { year: '2021', country: 'others', homicides: 0.04 } 
];

我尝试使用这个问题的答案,但代码不允许我设置最大类别数。

谢谢!

javascript arrays aggregate lodash
1个回答
0
投票

从我上面的评论...

@AbdullahCh ...如果是这样的话将年度特定凶杀案计数限制为仅 6 个条目,OP 希望仅过滤前 5 个年度特定条目;所有其他条目都加入国家/地区:“其他”,其中包含所有被拒绝/截断的条目的年度特定凶杀案总数。

+ + + 解释正在进行中 + + +

function aggregateLimitedNumberOfAnnualSpecificItems(collector = {}, item) {

  // access the result object and the item limit from the collector object.
  const { result = {}, limit = Number.MAX_SAFE_INTEGER } = collector;

  // access an item's year value.
  const { year } = item;

  // create or/and access the year-specific items array.
  const annualItemList = (result[year] ??= []);


  if (annualItemList.length < (limit - 1)) {
    // ... either collect unlimited yet ...

    annualItemList.push(item);

  } else if (annualItemList.length === (limit - 1)) {
    // ... or prepare the last item ...
  
    annualItemList.push({ ...item, country: 'others' });
  } else {
    // ... which then will aggregate specifc data from
    //     every still to be processed but limited item ...
    const lastItem = annualItemList.at(-1);
    
    lastItem.homicides =
      Math.round((lastItem.homicides + item.homicides) * 100) / 100;
  }
  // proceed with or return the year-specific
  // collection/aggregation of limited items.
  return { limit, result };
}

console.log(
  Object
    .values(
      data
        .reduce(aggregateLimitedNumberOfAnnualSpecificItems, {
          limit: 6, result: {},
        })
        .result
    )
    .flat()
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
<script>
  const data = [ 
    { year: '2020', country: 'Brazil', homicides: 0.60 },
    { year: '2020', country: 'Argentina', homicides: 0.10 }, 
    { year: '2020', country: 'Venezuela', homicides: 0.09 }, 
    { year: '2020', country: 'Uruguay', homicides: 0.08 }, 
    { year: '2020', country: 'Paraguay', homicides: 0.04 }, 
    { year: '2020', country: 'Bolivia', homicides: 0.03 }, 
    { year: '2020', country: 'Peru', homicides: 0.02 }, 
    { year: '2020', country: 'Chile', homicides: 0.02 },
    { year: '2020', country: 'Equador', homicides: 0.02 }, 
    { year: '2021', country: 'Brazil', homicides: 0.70 }, 
    { year: '2021', country: 'Venezuela', homicides: 0.10 }, 
    { year: '2021', country: 'Ecuador', homicides: 0.08 }, 
    { year: '2021', country: 'Argentina', homicides: 0.06 }, 
    { year: '2021', country: 'Paraguay', homicides: 0.02 }, 
    { year: '2021', country: 'Uruguay', homicides: 0.02 }, 
    { year: '2021', country: 'Chile', homicides: 0.01 },   
    { year: '2021', country: 'Peru', homicides: 0.01 },
  ];
</script>

© www.soinside.com 2019 - 2024. All rights reserved.