1. Comparison of data before and after deduplication// The original data is like this // The data after deduplication is like this [{ [{ "goodsId": "1", "goodsId": "1", "quota": 12, "quota": 12, "skuId": "1" }, }, { { "goodsId": "2", "goodsId": "2", "quota": 12, "quota": 12, "skuId": "2" }, }] { "goodsId": "1", "quota": 12, "skuId": "1" }] 2. How to use
Conclusion: The time difference between filter and reduce is not too big. Filter is slightly faster, but the filter syntax is more concise. 1. Use filter and MapThe code is concise and easy to use. It can be done in 4 lines of code. The average time spent is the shortest. Five-star recommendation function uniqueFunc(arr, uniId){ const res = new Map(); return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1)); } 2. Use reduceA little more code, average time spent is on par with the first one, four-star recommendation function uniqueFunc2(arr, uniId){ let hash = {} return arr.reduce((accum,item) => { hash[item[uniId]] ? '' : hash[item[uniId]] = true && accum.push(item) return accum },[]) } 3. Using for loopIt takes a little more time than the first and second ones, but the time spent is average, Samsung recommends function uniqueFunc3(arr, uniId){ let obj = {} let tempArr = [] for(var i = 0; i<arr.length; i++){ if(!obj[arr[i][uniId]]){ tempArr.push(arr[i]) obj[arr[i][uniId]] = true } } return tempArr } 3. Comparison of processing time of three methods for 2400 data
Test time screenshot display SummarizeThis concludes the article on 3 methods and comparisons of JS object array deduplication. For more relevant JS object array deduplication content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation code for installing vsftpd in Ubuntu 18.04
>>: A brief introduction to MySQL dialect
Method 1: Use the SET PASSWORD command MySQL -u r...
Table of contents Introduction Example: Event del...
When threads execute concurrently, we need to ens...
1. Create a SpringBooot project and package it in...
How to create a virtual machine on VMware and ins...
Introduction: AD is the abbreviation of Active Di...
Python connects to MySQL to modify and query data...
Table of contents Install mysql Configuring envir...
Here's the thing: Everyone knows about "...
Table of contents Solution, Summarize: vue projec...
When configuring proxy_pass in nginx, if you matc...
Note: When writing the docker-compose.yml file, a...
Table of contents 1. DateTimePicker date selectio...
I started configuring various environments this a...
This article records the graphic tutorial of MySQ...