Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!