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

Multiple methods to modify MySQL root password (recommended)

Method 1: Use the SET PASSWORD command MySQL -u r...

Detailed examples of using JavaScript event delegation (proxy)

Table of contents Introduction Example: Event del...

How to deploy SpringBoot project using Dockerfile

1. Create a SpringBooot project and package it in...

Steps to set up Windows Server 2016 AD server (picture and text)

Introduction: AD is the abbreviation of Active Di...

Python MySQL database table modification and query

Python connects to MySQL to modify and query data...

An exploration of the JS operator in problem

Here's the thing: Everyone knows about "...

In-depth study of vue2.x--Explanation of the h function

Table of contents Solution, Summarize: vue projec...

Detailed explanation of common template commands in docker-compose.yml files

Note: When writing the docker-compose.yml file, a...

Summarize the problems encountered in using Vue Element UI

Table of contents 1. DateTimePicker date selectio...

MySQL 8.0.17 installation and configuration graphic tutorial

This article records the graphic tutorial of MySQ...