-2

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

1
  • why dont you first filter out flag value false and then do the filter+findindex only for documentTypeId? Commented May 3, 2023 at 15:34

2 Answers 2

0

I am not sure what your goal here is. You might be asking the question wrong. I think what you might be looking at is to remove duplicates from an array based on flags documentTypeId and flag with another condition that if there are duplicates you want to prefer the document which has flag=true.

You could try something like arranging all documentTypeIds in an array and sort them with boolean as first element and pick all documentTypeIds first element after the sort. Like below

const filterFn = (arr) => Object.values(
    arr.reduce((acc, cur) => ({
      ...acc,
      [cur.documentTypeId]: [...acc[cur.documentTypeId] || [], cur]
    }), {}))
  .map(i => i.sort(({flag: a}, {flag: b}) => Number(b) - Number(a))[0]);

const test1 = [{
    "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 test2 = [{
    "docName": "test1",
    "active": true,
    "documentTypeId": 3,
    "flag": false
  },
  {
    "docName": "test2",
    "active": true,
    "documentTypeId": 1,
    "flag": false
  },
  {
    "docName": "test3",
    "documentTypeId": 5,
    "id": 5,
    "flag": false
  },
  {
    "docName": "test4",
    "active": true,
    "documentTypeId": 16,
    "flag": false
  },
  {
    "docName": "test5",
    "documentTypeId": 16,
    "id": 102,
    "flag": true
  }
]
const filterFn = (arr) => Object.values(
    arr.reduce((acc, cur) => ({
      ...acc,
      [cur.documentTypeId]: [...acc[cur.documentTypeId] || [], cur]
    }), {}))
  .map(i => i.sort(({flag: a}, {flag: b}) => Number(b) - Number(a))[0]);

console.log(filterFn(test1));
console.log(filterFn(test2));

Sign up to request clarification or add additional context in comments.

3 Comments

For below array it is not working -
[{ "docName": "test1", "active": true, "documentTypeId": 3, "flag": false }, { "docName": "test2", "active": true, "documentTypeId": 1, "flag": false }, { "docName": "test3", "documentTypeId": 5, "id": 5, "flag": false }, { "docName": "test4", "active": true, "documentTypeId": 16, "flag": false }, { "docName": "test5", "documentTypeId": 16, "id": 102, "flag": true } ]
@UIAPPDEVELOPER check now. hope this helps
-1
const data = [
  {
    "id": 100,
    "documentTypeId": 3,
    "docName": "test",
    "flag": false
  }, {
    "id": 31184688089,
    "documentTypeId": 1,
    "docName": "test2",
    "flag": true
  },
  {
    "documentTypeId": 3,
    "docName": "test3",
    "flag": false,
    "active": true
  }
];

let typeMem = [];
let filtered = data.filter(({ flag: f, documentTypeId: d }) => 
  f || (typeMem[d] = (typeMem[d] || 0) + 1) && typeMem[d] < 2);

console.log(filtered);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.