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
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;
})