Navigate back to the homepage

how to remove duplicates from array in react native?

Infinitbility
React
August 4th, 2022 · 1 min read
how to remove duplicates from array in react native?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To remove duplicates from array in react native, use the new Set() method with it will create new array without dublicate elements.

The Set object lets you store unique values of any type, whether primitive values or object references.

To use new set in array you have to follow below syntax.

1[...new Set(arr)]

Today, I’m going to show you How do I remove duplicates from array in react native, as above mentioned, I’m going to use the above-mentioned new Set() method to get only unique elements of array.

Let’s start today’s tutorial how do you remove duplicates from array in react native?

React native remove duplicates from array example

Here, we will create an array with some data, and then we will use the new Set() method to remove duplicates elements and store in a different variable.

1import React, { useState, useEffect } from "react";
2import { View, Text } from "react-native";
3export default function App() {
4 const [arr, setArr] = useState([
5 "Infinitbility",
6 "aguidehub",
7 "sortoutcode",
8 "sortoutcode",
9 "Infinitbility"
10 ]);
11
12 useEffect(() => {
13 setArr([...new Set(arr)]);
14 }, []);
15
16 return (
17 <View>
18 <Text>{JSON.stringify(arr)}</Text>
19 </View>
20 );
21}

In the above example, I have created an array state with dublicate elements and make unique in a useEffect after that printed result in a screen.

I hope it helps you, All the best 👍.

React Native, remove duplicates from array example
React Native, remove duplicates from array example

Try it yourself

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to remove duplicates from array in typescript?

how to remove duplicates from array in typescript?

An example of remove duplicates from array in typescript with the step-by-step guide.

August 3rd, 2022 · 1 min read
how to empty an array in javascript?

how to empty an array in javascript?

An example of empty an array in javascript with the step-by-step guide.

August 2nd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]