亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
1. Using Set (Simple and Fast)
2. Using filter() and indexOf()
3. Using reduce() to Build Unique Array
4. Removing Duplicates from Object Arrays
Summary of Best Use Cases:
Home Web Front-end JS Tutorial How to remove duplicate values from an array in JS?

How to remove duplicate values from an array in JS?

Jul 30, 2025 am 03:57 AM
Array deduplication

Use Set to quickly remove duplicate original values in the array, such as [...new Set(arr)]; 2. Use filter() and indexOf() to retain the first occurrence of elements; 3. Use reduce() to manually build a non-repetitive array, but the performance is poor; 4. Object arrays need to be compared with filter() and findIndex() to de-heavy specific keys. For most cases, [...new Set(arr)] is the best choice, suitable for raw type data deduplication, while object arrays need to be filtered based on unique keys. The final method selection depends on the data type and performance requirements.

How to remove duplicate values from an array in JS?

There are several ways to remove duplicate values from an array in JavaScript. Here are the most common and effective methods:

How to remove duplicate values from an array in JS?

1. Using Set (Simple and Fast)

The easiest way is to use the Set object, which only stores unique values, then convert it back to an array.

 const arr = [1, 2, 2, 3, 4, 4, 5];
const unique = [...new Set(arr)];
console.log(unique); // [1, 2, 3, 4, 5]

This method is fast and clean for primitive values like strings, numbers, and booleans.

How to remove duplicate values from an array in JS?

Note: This only works reasonably for primitive types . For objects, it won't detect duplicates based on content.


2. Using filter() and indexOf()

This method keeps the first occurrence of each value and filters out later duplicates.

How to remove duplicate values from an array in JS?
 const arr = [1, 2, 2, 3, 4, 4, 5];
const unique = arr.filter((item, index) => arr.indexOf(item) === index);
console.log(unique); // [1, 2, 3, 4, 5]

How it works:

  • indexOf(item) returns the first index where the item appears.
  • If the current index matches that first index, it's the first occurrence — so we keep it.

This is useful when you want more control or are working in older environments (though Set is supported in modern browsers).


3. Using reduce() to Build Unique Array

You can use reduce() to manually build a new array without duplicates.

 const arr = [1, 2, 2, 3, 4, 4, 5];
const unique = arr.reduce((acc, curr) => {
  if (!acc.includes(curr)) {
    acc.push(curr);
  }
  return acc;
}, []);
console.log(unique); // [1, 2, 3, 4, 5]

While flexible, this method is slower for large arrays because includes() checks the entire accumulator each time.


4. Removing Duplicates from Object Arrays

For arrays of objects, you need to define what makes them "duplicates." Normally, you compare a specific key.

 const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice' }
];

const unique = users.filter((user, index, self) =>
  index === self.findIndex(u => u.id === user.id)
);

This keeps only the first object with each id . You can change u.id === user.id to compare other properties or multiple fields.


Summary of Best Use Cases:

  • Primitive values (numbers, strings): Use Set — it's fastest and cleanest.
  • Need compatibility or custom logic: Use filter() indexOf() .
  • Objects: Use filter() with findIndex() or a Map for better performance.
  • Large datasets: Avoid includes() or indexOf() in loops — use Map or Set with keys.

Basically, for most cases, [...new Set(arr)] is your go-to solution.

The above is the detailed content of How to remove duplicate values from an array in JS?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement array deduplication in es5 and es6 How to implement array deduplication in es5 and es6 Jan 16, 2023 pm 05:09 PM

In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax "for(i=0;i<array length;i++){a=newArr.indexOf(arr[i]);if(a== -1){...}}". In es6, you can use the spread operator, Array.from() and Set to remove duplication; you need to first convert the array into a Set object to remove duplication, and then use the spread operator or the Array.from() function to convert the Set object back to an array. Just group.

Specify the basis for removing duplicate elements when deduplicating PHP arrays Specify the basis for removing duplicate elements when deduplicating PHP arrays Apr 28, 2024 pm 10:48 PM

PHP's array_unique() function is used to remove duplicate elements from an array. By default, strict equality (===) is used. We can specify the basis for deduplication through a custom comparison function: create a custom comparison function and specify the deduplication standard (for example, based on element length); pass the custom comparison function as the third parameter to the array_unique() function. Remove duplicate elements based on specified criteria.

How to maintain key-value correspondence after deduplication in PHP array? How to maintain key-value correspondence after deduplication in PHP array? Apr 27, 2024 pm 12:33 PM

Methods to maintain key-value correspondence after PHP array deduplication include: use the array_unique() function to remove duplicate values, and then use the array_flip() function to exchange key-value pairs. Merge the original array with the deduplicated array, and use the array merging method to retain key-value correspondence.

How to remove duplicates from an array based on specific key-value pairs in PHP? How to remove duplicates from an array based on specific key-value pairs in PHP? Apr 28, 2024 pm 06:18 PM

In PHP, use the array_unique() function to remove duplicates from an array based on specific key-value pairs. When calling the function, pass in the array as a parameter and select the sorting method as the second parameter. This function returns a new array in which duplicates have been removed based on the specified key-value pairs.

Tips for handling empty values ??and null values ??when deduplicating PHP arrays Tips for handling empty values ??and null values ??when deduplicating PHP arrays Apr 26, 2024 pm 05:03 PM

Tips for handling empty and null values ??when deduplicating PHP arrays: Use array_unique with array_filter to filter out empty and null values. Use array_unique and define a custom comparison function to treat empty and null values ??as equal. Use array_reduce to iterate over an array and add items if they do not contain empty or null values.

How to remove duplicate elements from an array in php How to remove duplicate elements from an array in php May 25, 2023 pm 05:19 PM

How to remove duplicate elements in an array in PHP: 1. Use the "array_unique()" function to remove duplicate data in the array; 2. Traverse through a foreach loop and define a new array to store non-duplicate data to achieve deduplication; 3. Use the array_flip() and array_keys() functions to get the deduplicated array; 4. Use the array_filter() function to deduplicate the original array by using this function combined with an anonymous function.

How to exclude duplicate arrays in PHP How to exclude duplicate arrays in PHP Jun 05, 2023 pm 02:53 PM

Methods to exclude duplicate arrays in PHP: 1. Create a PHP sample file; 2. Define the array to be deduplicated as "$oldArr" and the new array after deduplication as "$newArr"; 3. Use "array_unique()" The function removes duplicate elements from the array and returns the deduplicated array. The code is "$newArr = array_unique($oldArr);" to eliminate duplicate elements. 4. Deduplication can also be performed through a for loop.

PHP array deduplication and retain the number of duplicate elements PHP array deduplication and retain the number of duplicate elements Apr 27, 2024 am 11:06 AM

In PHP, you can use the array_count_values() function to deduplicate an array and retain the number of repeated elements. This function returns an associative array where the keys are the elements in the original array and the values ??are the number of times those elements occur.

See all articles