4

JavaScript 新提案:array.groupBy()

 2 years ago
source link: https://www.fly63.com/article/detial/11334
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

许多开发人员喜欢 Ruby 编程语言,因为它具有丰富的标准实用程序库。例如,Ruby中的数组有大量的方法。

不过,我们的JavaScript也在努力,在字符串和数组方面逐步丰富了它的标准库。例如,在以前的文章中,介绍新的 array.at() 方法。

今天我们在来看新的数组组提案(目前处于第三阶段),它引入了新方法 array.groupby() 和array.groupbytomap() 。它们的 polyfills 文件可以在 core-js 库中找到。

接着,我们来看下能从中学到些什么。

1. array.groupBy()

假设我们有一个产品列表,其中每个产品都是一个具有2个属性的对象: name 和 category。

const products = [
  { name: 'apples', category: 'fruits' },
  { name: 'oranges', category: 'fruits' },
  { name: 'potatoes', category: 'vegetables' }
];

在上面的示例中,products 是一个产品对象数组。

现在,对产品列表执行一个简单的操作,将产品按类别分组。

const groupByCategory = {
  'fruits': [
    { name: 'apples', category: 'fruits' }, 
    { name: 'oranges', category: 'fruits' },
  ],
  'vegetables': [
    { name: 'potatoes', category: 'vegetables' }
  ]
};

如何从 products 数组中得到一个类似 groupByCategory 的数组?

通常的方法是使用array.reduce()来实现,如下所示:

const groupByCategory = products.reduce((group, product) => {
  const { category } = product;
  group[category] = group[category] ?? [];
  group[category].push(product);
  return group;
}, {});
console.log(groupByCategory);
// {
//   'fruits': [
//     { name: 'apples', category: 'fruits' }, 
//     { name: 'oranges', category: 'fruits' },
//   ],
//   'vegetables': [
//     { name: 'potatoes', category: 'vegetables' }
//   ]
// }

products.reduce((acc, product) => { ... }) 将产品数组还原为一个按类别分组的产品对象。

array.reduce()方法有用且强大,但有时它的可读性并不是最好的。

因为分组数据是常见的事(从SQL中召回groupby ?),数组组提案引入了两个有用的方法:array. groupBy()和 array.groupByToMap()。

下面介绍如何使用 array.groupBy() 创建相同的分类分组:

const groupByCategory = products.groupBy(product => {
  return product.category;
});

console.log(groupByCategory); 

// {
//   'fruits': [
//     { name: 'apples', category: 'fruits' }, 
//     { name: 'oranges', category: 'fruits' },
//   ],
//   'vegetables': [
//     { name: 'potatoes', category: 'vegetables' }
//   ]
// }

products.groupBy(product => {...}) 返回一个对象,其中每个属性的键是类别名称,值是对应类别的产品数组。

使用 products.groupBy() 分组比使用 product.reduce() 代码更少,更容易理解。

array.groupBy(callback) 接受一个回调函数,该函数被调用时有3个参数:当前数组项、索引和数组本身。回调函数应该返回一个字符串:你想添加项目的组名。

const groupedObject = array.groupBy((item, index, array) => {
  // ...
  return groupNameAsString;
});

2. array.groupByToMap()

有时你可能想使用 Map 而不是普通对象。 Map 的好处是它可以接受任何数据类型作为键,但普通对象只限于字符串和 symbol。

恩,如果你想把数据分组到一个Map中,你可以使用 array.groupByToMap() 方法。

array.groupByToMap(callback)的工作方式与 array.groupBy(callback) 完全一样,只是它将项目分组到 Map 中,而不是一个普通的 JS 对象中。

例如,将产品数组按类别名称分组到一个 ap 中,执行方法如下。

const groupByCategory = products.groupByToMap(product => {
  return product.category;
});

console.log(groupByCategory); 

// Map([
//   ['fruits', [
//     { name: 'apples', category: 'fruits' }, 
//     { name: 'oranges', category: 'fruits' },
//   ]],
//   ['vegetables', [
//     { name: 'potatoes', category: 'vegetables' }
//   ]
// ])

如果你想轻松地对数组中的项进行分组(类似于SQL中的GROUP BY),那么欢迎使用新方法array.groupBy() 和 array.groupByToMap()。

两个函数都接受一个回调函数,该回调函数应返回必须插入当前项的组的键。

array.groupBy()将这些项分组为一个普通的JavaScript对象,而array.groupByToMap()将它们分组为一个 Map 实例。

如果你想马上使用这些函数,那么使用 core-js 库提供的 polyfill。

原文:https://dmitripavlutin.com/javascript-array-group/
译者:前端小智

链接: https://www.fly63.com/article/detial/11334


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK