I have an array of object , I want to remove duplicate one based on ‘documentTypeId’ and ‘flag’ value. Here in below object I want to remove the object having ‘documentTypeId’ value is same and flag value is false. I tried but not getting proper filter. Please find the below code.
const test = [{
"id": 100,
"documentTypeId": 3,
"docName": "test",
"flag": false
}, {
"id": 31184688089,
"documentTypeId": 1,
"docName": "test2",
"flag": true
},
{
"documentTypeId": 3,
"docName": "test3",
"flag": true,
"active": true
}
]
const res = test.filter((value, index, self) =>
index === self.findIndex((t) => (
t.documentTypeId === value.documentTypeId && t.flag == false
))
)
console.log(res);
2
Answers
Filter all true values the run it
Use
value.flag
instead of hardcodedfalse
Hope it works now