Examples and comparison of 3 methods for deduplication of JS object arrays

Examples and comparison of 3 methods for deduplication of JS object arrays

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

  1. Using filter and Map 🌟🌟🌟🌟🌟
  2. Using reduce 🌟🌟🌟🌟
  3. for loop 🌟🌟🌟

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 Map

The 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 reduce

A 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 loop

It 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

Number of tests filter Map reduce for loop
1 0.139892578125 ms 0.19189453125 ms 0.2060546875 ms
2 0.12109375 ms 0.1279296875 ms 0.195068359375 ms
3 0.112060546875ms 0.11767578125 ms 0.174072265625 ms
4 0.10400390625 ms 0.1728515625 ms 0.18701171875 ms
5 0.10986328125 ms 0.12890625 ms 0.175048828125 ms
6 0.113037109375 ms 0.10791015625 ms 0.172119140625 ms
7 0.134033203125 ms 0.129150390625 ms 0.172119140625 ms

Test time screenshot display

Summarize

This 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:
  • js object to achieve data paging effect
  • Detailed explanation of the order of JS object traversal
  • When to use Map instead of plain JS objects
  • JS object copying (deep copy and shallow copy)
  • Example code for converting camel case to underline in js object attribute name
  • Detailed example of reading speed of js objects

<<:  Implementation code for installing vsftpd in Ubuntu 18.04

>>:  A brief introduction to MySQL dialect

Recommend

A brief discussion on several situations where MySQL returns Boolean types

mysql returns Boolean type In the first case, ret...

Webpack loads css files and its configuration method

webpack loads css files and its configuration Aft...

CSS3 realizes the animation of shuttle starry sky

Result: html <canvas id="starfield"&...

SQL Server Comment Shortcut Key Operation

Batch comments in SQL Server Batch Annotation Ctr...

How to configure Nginx virtual host in CentOS 7.3

Experimental environment A minimally installed Ce...

Can't connect to local MySQL through socket '/tmp/mysql.sock' solution

Error message: ERROR 2002: Can't connect to l...

Advanced crawler - Use of Scrapy_splash component for JS automatic rendering

Table of contents 1. What is scrapy_splash? 2. Th...

Detailed Example of CSS3 box-shadow Property

CSS3 -- Adding shadows (using box shadows) CSS3 -...

Detailed examples of converting rows to columns and columns to rows in MySQL

mysql row to column, column to row The sentence i...

Design theory: people-oriented green design

Reflections on the two viewpoints of β€œpeople-orie...

Method for comparing the size of varchar type numbers in MySQL database

Create a test table -- --------------------------...

Docker removes abnormal container operations

This rookie encountered such a problem when he ju...

Implementation of Nginx configuration of local image server

Table of contents 1. Introduction to Nginx 2. Ima...

Introduction to major browsers and their kernels

Trident core: IE, MaxThon, TT, The World, 360, So...