Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
How to remove duplicate value in array javascript?

I've got data like this :

data = [
    {
        "emp_id": 1,
        "hashtag_id": [
            1,
            3,
            3,
            4,
            2,
            2,
            1,
            1
        ]
    },
    {
        "emp_id": 2,
        "hashtag_id": [
            1,
            1
        ]
    },
    {
        "emp_id": 3,
        "hashtag_id": [
            3,
            1
        ]
    },
    {
        "emp_id": 4,
        "hashtag_id": [
            1,
            3,
            4
        ]
    },
    {
        "emp_id": 5,
        "hashtag_id": 1
    },
    {
        "emp_id": 6,
        "hashtag_id": [
            1,
            4
        ]
    }
]

I want to remove duplicate value in hashtag_id if its exist.

this is what i expected :

data = [
    {
        "emp_id": 1,
        "hashtag_id": [
            1,
            3,
            4,
            2,
        ]
    },
    {
        "emp_id": 2,
        "hashtag_id": [
            1
        ]
    },
    {
        "emp_id": 3,
        "hashtag_id": [
            3,
            1
        ]
    },
    {
        "emp_id": 4,
        "hashtag_id": [
            1,
            3,
            4
        ]
    },
    {
        "emp_id": 5,
        "hashtag_id": 1
    },
    {
        "emp_id": 6,
        "hashtag_id": [
            1,
            4
        ]
    }
]

I was trying with this :

data = data.map(item => {
        item.hashtag_id = [...new Set(item.hashtag_id)]
        return item
      })

But got error :

"number 1 is not iterable (cannot read property Symbol(Symbol.iterator))"

What is wrong with that ?

Please tell me if you need more information to solve that problem if it's still not enough

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In the item before the last, hashtag_id is a number instead of an array of numbers. Since a number isn't an iterable, you can't make a Set of it causing the error.

To account for the possibility of hashtag_id being a number, do this:

data = data.map((item) => {
    item.hashtag_id = Array.isArray(item.hashtag_id) ? [...new Set(item.hashtag_id)] : item.hashtag_id;
    return item;
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda