• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共299篇

    前端 - Javascript

关闭

返回栏目

关闭

返回前端 - Javascript栏目

247 - 数组分组 - 数组分组应用 - 数据分类统计示例

作者:

贺及楼

成为作者

更新日期:2025-02-21 21:04:12

数组分组 - 数组分组应用 - 数据分类统计示例

在前端开发中,处理数据是一项常见且重要的任务。而数组作为 JavaScript 中最常用的数据结构之一,对数组进行分组和分类统计是非常实用的操作。本文将深入探讨数组分组的方法,并通过实际的示例展示如何应用数组分组进行数据分类统计。

为什么需要数组分组和分类统计

在实际开发中,我们经常会遇到需要将数据按照某种规则进行分组,并对每组数据进行统计分析的情况。例如,在一个电商网站中,我们可能需要统计每个商品类别的销售数量;在一个学生成绩管理系统中,我们可能需要统计每个班级的平均分等。通过数组分组和分类统计,我们可以更清晰地了解数据的分布情况,为后续的决策提供有力支持。

数组分组的方法

1. 使用 for 循环

这是最基本的方法,通过遍历数组,根据分组规则将元素添加到不同的组中。

  1. // 示例数据
  2. const products = [
  3. { name: 'iPhone', category: '手机' },
  4. { name: 'iPad', category: '平板' },
  5. { name: 'MacBook', category: '笔记本电脑' },
  6. { name: '小米手机', category: '手机' }
  7. ];
  8. // 分组结果对象
  9. const groupedProducts = {};
  10. // 遍历数组
  11. for (let i = 0; i < products.length; i++) {
  12. const product = products[i];
  13. const category = product.category;
  14. // 如果该类别还没有分组,则创建一个新的数组
  15. if (!groupedProducts[category]) {
  16. groupedProducts[category] = [];
  17. }
  18. // 将产品添加到对应的分组中
  19. groupedProducts[category].push(product);
  20. }
  21. console.log(groupedProducts);

2. 使用 reduce 方法

reduce 方法是 JavaScript 数组的一个强大方法,可以将数组的元素依次处理并返回一个累加值。我们可以利用 reduce 方法来实现数组分组。

  1. const groupedProducts2 = products.reduce((acc, product) => {
  2. const category = product.category;
  3. acc[category] = acc[category] || [];
  4. acc[category].push(product);
  5. return acc;
  6. }, {});
  7. console.log(groupedProducts2);

两种方法的比较

方法 优点 缺点
for 循环 直观易懂,适合初学者 代码量相对较多,不够简洁
reduce 方法 代码简洁,函数式编程风格 对于不熟悉 reduce 方法的开发者来说,理解起来有一定难度

数据分类统计示例

统计每个商品类别的数量

  1. const categoryCount = {};
  2. for (let i = 0; i < products.length; i++) {
  3. const product = products[i];
  4. const category = product.category;
  5. if (!categoryCount[category]) {
  6. categoryCount[category] = 0;
  7. }
  8. categoryCount[category]++;
  9. }
  10. console.log(categoryCount);

使用 reduce 方法统计每个商品类别的数量

  1. const categoryCount2 = products.reduce((acc, product) => {
  2. const category = product.category;
  3. acc[category] = (acc[category] || 0) + 1;
  4. return acc;
  5. }, {});
  6. console.log(categoryCount2);

统计每个班级学生的平均分

  1. const students = [
  2. { name: '张三', class: '一班', score: 80 },
  3. { name: '李四', class: '二班', score: 90 },
  4. { name: '王五', class: '一班', score: 70 }
  5. ];
  6. const classScores = students.reduce((acc, student) => {
  7. const className = student.class;
  8. if (!acc[className]) {
  9. acc[className] = { totalScore: 0, count: 0 };
  10. }
  11. acc[className].totalScore += student.score;
  12. acc[className].count++;
  13. return acc;
  14. }, {});
  15. const classAverages = {};
  16. for (const className in classScores) {
  17. const { totalScore, count } = classScores[className];
  18. classAverages[className] = totalScore / count;
  19. }
  20. console.log(classAverages);

总结

数组分组和分类统计是前端开发中非常实用的技巧。通过 for 循环和 reduce 方法,我们可以方便地对数组进行分组,并对每组数据进行统计分析。在实际开发中,我们可以根据具体情况选择合适的方法。希望本文的示例能够帮助你更好地理解和应用数组分组和分类统计。