当前位置:网站首页>Fast classification of array.group() in ES6
Fast classification of array.group() in ES6
2022-07-28 13:16:00 【Pig Xiaoyong】
List of articles
ES6 Medium Array.group() Quickly sort and combine arrays
1. Need to rely on babel, Install... In the project first
npm i core-js
2. introduce
import 'core-js/actual/array/group
3. Application scenarios
I have an array , According to some information in the array , Grouping .
example :
There is a set of customer information , Now it will be grouped according to provinces .
4. application
One line of code to solve
const orderList = [{
nickName: 'steven',
productName: ' watermelon ',
price: 29,
province: 'henan',
},{
nickName: ' The other person's ',
productName: ' Arbutus ',
price: 22,
province: 'shanxi',
},{
nickName: ' Fan battery ',
productName: ' Apple ',
price: 19,
province: 'dongbei',
},{
nickName: ' survey v',
productName: ' Peach ',
price: 88,
province: 'shanxi',
},{
nickName: '2 Number ',
productName: ' Peach ',
price: 88,
province: 'shanxi',
}]
const group=orderList.group(({
province})=>province)
console.log(group)

5. Implementation when not in use
const provinceObj = {
}
for (let index = 0; index < orderList.length; index++) {
const element = orderList[index]; // This data of the cycle object
const provinceKey = element.province // The province field in this data
// Determine whether the attribute exists in the empty object , Yes key Values exist push
if (provinceObj[provinceKey]) {
provinceObj[provinceKey].push(element)
} else {
// key Create a new one if the value does not exist key and value
provinceObj[provinceKey] = [element] //[element] Put this data into the array
}
}
console.log(provinceObj)
The results are the same 
边栏推荐
- Fundamentals of machine learning - principal component analysis pca-16
- Shenwenbo, researcher of the Hundred Talents Program of Zhejiang University: kernel security in the container scenario
- 《TiDB 6.x in Action》发布,凝聚社区集体智慧的 6.x 实践汇总!
- [June 28 event preview] low code Summit
- [embedded C foundation] Part 3: constants and variables
- 03 pyechars rectangular coordinate system chart (example code + effect drawing)
- Pointnet++ Chinese Translation
- Qt 信号和槽机制( 详解 )
- How does kotlin help you avoid memory leaks?
- [FPGA]: AD sampling problem
猜你喜欢

【嵌入式C基础】第1篇:基本数据类型

SQL most commonly used basic operation syntax

Machine learning practice - logistic regression-19

Redefinition problem of defining int i variable in C for loop
![[pictures and texts] detailed tutorial of one click reinstallation of win11 system](/img/cc/749fe4095fc5afb1fc2c65df43d06c.png)
[pictures and texts] detailed tutorial of one click reinstallation of win11 system

Introduction to border border attribute

UV germicidal lamp chip dlt8p65sa Jericho
![[FPGA] FIR filter - half band filter](/img/6e/d97b3842f80e37aa41b888384a14cb.png)
[FPGA] FIR filter - half band filter

机器学习基础-集成学习-13

【C语言易错点】第4篇:结构体在内存中存储规则详讲
随机推荐
Black cat takes you to learn EMMC Protocol Part 24: detailed explanation of EMMC bus test program (cmd19 & cmd14)
How many times can the WordPress user name be changed? Attach the method of changing user name
子线程更新UI全解
Use and source code of livedata in jetpack family bucket
[embedded C foundation] Part 6: super detailed explanation of common input and output functions
2020-12-27
Smart touch screen LCD bathroom mirror light touch chip-dlt8t02s-jericho
Dimming and color matching cool light touch chip-dlt8ma12ts-jericho
LeetCode·每日一题·1331.数组序号转换·离散化
Full disclosure! Huawei cloud distributed cloud native technology and Practice
Databinding+LiveData轻松实现无重启换肤
Comments are not allowed in JSON
Android engineers, how to use kotlin to provide productivity?
[embedded C foundation] Part 7: detailed introduction to C language process control
我抄底了被清算的NFT,却被OpenSea上了锁
SSH port forwarding (Tunneling Technology)
黑猫带你学eMMC协议第27篇:什么是eMMC的动态容量(Dynamic Capacity)?
Flexpro software: measurement data analysis in production, research and development
CTO of youhaoda, MVP of Huawei cloud, and Zhang Shanyou: build cloud native applications based on kubernetes and dapr
How to open the power saving mode of win11 system computer