Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda