🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenVorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf(11.09.2026 um 09:35 Uhr)
🕵️ SicherheitslückenMicrosoft geht endlich eines der nervigsten Probleme von Windows 11 an(11.09.2026 um 11:58 Uhr)
💾 IT Security ToolsSysinternals Suite(11.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)
🔧 AI Nachrichten Stealing AI Reasoning Traces(08.09.2026 um 12:20 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenVorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf(11.09.2026 um 09:35 Uhr)
🕵️ SicherheitslückenMicrosoft geht endlich eines der nervigsten Probleme von Windows 11 an(11.09.2026 um 11:58 Uhr)
💾 IT Security ToolsSysinternals Suite(11.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)
🔧 AI Nachrichten Stealing AI Reasoning Traces(08.09.2026 um 12:20 Uhr)

🔧 Programmierung 🕛 vor 2 Jahren 3 Min Lesezeit
0

Remove Duplicates Ways from an Array in Javascript

↗ Quelle (dev.to)
🗣️ Stimme:

Duplicate elements can be a nuisance when working with arrays in JavaScript. They not only clutter your data but can also affect the performance of your code. Fortunately, JavaScript offers several efficient methods to remove duplicates from arrays, each with its own advantages and use cases. In this article, we'll delve into six distinct approaches to remove duplicates from arrays, ranging from utilizing built-in methods like Set to more custom solutions for arrays of objects.






1-Using a Set



JavaScript Sets are collections of unique values, making them a natural choice for removing duplicates from arrays. By converting an array to a Set, all duplicate values are automatically discarded.




CODE
const arrayWithDuplicates = [1, 2, 3, 4, 4, 5, 6, 6];
const uniqueArray = [...new Set(arrayWithDuplicates)];
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5, 6]









2-Using indexOf() and filter() methods



The combination of indexOf() and filter() methods offers a straightforward approach to removing duplicates by filtering out elements based on their first occurrence in the array.




CODE
const arrayWithDuplicates = [1, 2, 3, 4, 4, 5, 6, 6];
const uniqueArray = arrayWithDuplicates.filter(
(value, index, array) => array.indexOf(value) === index
);
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5, 6]









3-Using Reduce



The reduce method allows for concise code to remove duplicates while preserving the order of elements in the original array.




CODE
const arrayWithDuplicates = [1, 2, 3, 4, 4, 5, 6, 6];
const uniqueArray = arrayWithDuplicates.reduce(
(accumulator, currentValue) => {
if (!accumulator.includes(currentValue)) {
accumulator.push(currentValue);
}
return accumulator;
}, []);
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5, 6]









4-Using forEach() and include()



Using forEach() along with includes() method, you can efficiently iterate over the array and check if an element is already present in the result array, thus removing duplicates.




CODE
const arrayWithDuplicates = [1, 2, 3, 4, 4, 5, 6, 6];
const uniqueArray = [];
arrayWithDuplicates.forEach((value) => {
if (!uniqueArray.includes(value)) {
uniqueArray.push(value);
}
});
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5, 6]









5-Remove duplicates from an array of objects by one property



When dealing with arrays of objects, you might want to remove duplicates based on a specific property. Here's how you can achieve that using the filter() method and a temporary object to track unique values.




CODE
const arrayOfObjectsWithDuplicates = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 1, name: 'John' }
];
const uniqueArray = arrayOfObjectsWithDuplicates.filter(
(obj, index, self) =>
index === self.findIndex((t) => t.id === obj.id)
);
console.log(uniqueArray);
// Output: [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
]










6-Remove duplicates from an array of objects by multiple properties



In scenarios where you need to consider multiple properties for removing duplicates from arrays of objects, you can create a composite key and utilize a similar approach as above.




CODE
const arrayOfObjectsWithDuplicates = [
{ id: 1, name: 'John', age: 30 },
{ id: 2, name: 'Jane', age: 25 },
{ id: 1, name: 'John', age: 30 }
];

const uniqueArray = arrayOfObjectsWithDuplicates.filter(
(obj, index, self) =>
index === self.findIndex(
(t) => t.id === obj.id &&
t.name === obj.name &&
t.age === obj.age
)
);
console.log(uniqueArray);
// Output: [
{ id: 1, name: 'John', age: 30 },
{ id: 2, name: 'Jane', age: 25 }
]









Conclusion



With these diverse techniques at your disposal, you can confidently tackle the task of removing duplicates from arrays in JavaScript. Whether you're working with simple arrays or complex arrays of objects, there's a suitable method to streamline your data and optimize your code's performance. Choose the approach that best fits your requirements and elevate your JavaScript programming skills to the next level.



References:



Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Text Watermarking in Python: Catch Whoever Copies Your Writing
1 Quelle
Why Most Multi-Agent Systems Fail Even When Evaluation Passes
1 Quelle
A Beginner’s Guide to World Models
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Remove Duplicates Ways from an Array in Javascript

Thematisch verwandte Begriffe: Remove, Duplicates, Ways, from · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...