I have object like this :
cons data = []
const result =
[
{
"result_id": "AAA877",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAA877",
"hashtag_id": 1,
"apptype_id": 3,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAAAA1",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAAAA1",
"hashtag_id": 1,
"apptype_id": 4,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAB238",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAB238",
"hashtag_id": 2,
"apptype_id": 4,
"tag": null
}
]
},
{
"result_id": "AAB415",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAB415",
"hashtag_id": 1,
"apptype_id": 3,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAD668",
"emp_id": 2,
"hashtag": [
{
"result_id": "AAD668",
"hashtag_id": 1,
"apptype_id": 3,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAG239",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAG239",
"hashtag_id": 4,
"apptype_id": 3,
"tag": null
}
]
},
{
"result_id": "AAH740",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAH740",
"hashtag_id": 2,
"apptype_id": 3,
"tag": null
}
]
},
{
"result_id": "AAK119",
"emp_id": 2,
"hashtag": [
{
"result_id": "AAK119",
"hashtag_id": 1,
"apptype_id": 4,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAK298",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAK298",
"hashtag_id": 2,
"apptype_id": 3,
"tag": null
}
]
}
]
I want to filter and push emp_id and apptype_id without a duplicate
This is what i expected :
[
{ emp_id: 1, app_type_id: 3 },
{ emp_id: 1, app_type_id: 4 },
{ emp_id: 2, app_type_id: 3 },
{ emp_id: 2, app_type_id: 4 }
]
I was trying like this :
result.forEach(r => {
if (r.hashtag[0].tag !== null) {
const t = {
emp_id: r.emp_id,
app_type_id: r.hashtag[0].apptype_id
}
if (data.indexOf(t) === -1) {
data.push(t)
}
}
})
But what i got was like this :
[
{ emp_id: 1, app_type_id: 3 },
{ emp_id: 1, app_type_id: 4 },
{ emp_id: 1, app_type_id: 3 },
{ emp_id: 2, app_type_id: 3 },
{ emp_id: 2, app_type_id: 4 }
]
How to filter without a duplicate like what i expected ?
Please ask me if you need more information if it's still not enough
hashtagalways only have one object in its array?hashtagwill always have one object in array @Andyhashtagarray always has one item in it, I would approach this withArray.reducemethod. That is, create a new array byreducemethod that organizes the data byemp_id, then build a final result array bymapthat massages the output.