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

Vue implements a simple marquee effect

This article shares the specific code of Vue to a...

Customization Method of Linux Peripheral File System

Preface Generally speaking, when we talk about Li...

MySQL fuzzy query usage (regular, wildcard, built-in function)

Table of contents 1. MySQL wildcard fuzzy query (...

Let you understand the deep copy of js

Table of contents js deep copy Data storage metho...

How to create a database in navicat 8 for mysql

When developing a website, you often need to use ...

Build Tomcat9 cluster through Nginx and realize session sharing

Use Nginx to build Tomcat9 cluster and Redis to r...

Vue el-date-picker dynamic limit time range case detailed explanation

There are two situations 1. Start time and end ti...

Navicat imports csv data into mysql

This article shares with you how to use Navicat t...

Detailed explanation of the pitfalls of MySQL 8.0

I updated MySQL 8.0 today. The first problem: Nav...

Using MySQL in Windows: Implementing Automatic Scheduled Backups

1. Write a backup script rem auther:www.yumi-info...

3 methods to restore table structure from frm file in mysql [recommended]

When mysql is running normally, it is not difficu...

How to add automatic completion commands for docker and kubectl on Mac

Introduction to kubectl kubectl is a command line...