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
mysql returns Boolean type In the first case, ret...
webpack loads css files and its configuration Aft...
Result: html <canvas id="starfield"&...
How to add a loading animation every time I scrol...
Batch comments in SQL Server Batch Annotation Ctr...
Experimental environment A minimally installed Ce...
Error message: ERROR 2002: Can't connect to l...
Table of contents 1. What is scrapy_splash? 2. Th...
CSS3 -- Adding shadows (using box shadows) CSS3 -...
mysql row to column, column to row The sentence i...
Reflections on the two viewpoints of βpeople-orie...
Create a test table -- --------------------------...
This rookie encountered such a problem when he ju...
Table of contents 1. Introduction to Nginx 2. Ima...
Trident core: IE, MaxThon, TT, The World, 360, So...