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
1. Filter Example: <!DOCTYPE html> <html...
This article shares the specific code of Vue to a...
Preface Generally speaking, when we talk about Li...
Table of contents 1. MySQL wildcard fuzzy query (...
Table of contents js deep copy Data storage metho...
When developing a website, you often need to use ...
Use Nginx to build Tomcat9 cluster and Redis to r...
This article shares the specific code of vue3 enc...
mysql gets all dates or months in a time period 1...
There are two situations 1. Start time and end ti...
This article shares with you how to use Navicat t...
I updated MySQL 8.0 today. The first problem: Nav...
1. Write a backup script rem auther:www.yumi-info...
When mysql is running normally, it is not difficu...
Introduction to kubectl kubectl is a command line...