Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove duplicates from array if value set for one of the duplicates, else hold null (javascript/typescript)

I need to remove duplicate or empty values (sets) from an array in javascript (typescript) if there is data with a non-empty value on the exact same date. If all the values (sets) were empty on that day, we should keep one value.

Example:

0: (2) ['2022-12-05', null]   ---> Remove due to "Reason 1"
1: (2) ['2022-12-05', 2695.923015841]   ---> Remain because it has a y value (on 5 Dec)
2: (2) ['2022-12-05', null]   ---> Remove due to "Reason 1"
3: (2) ['2022-12-05', null]   ---> Remove due to "Reason 1"

4: (2) ['2022-12-12', null]   ---> Remove due to "Reason 1"
5: (2) ['2022-12-12', 3984.864626441]   ---> Remain because it has a y value (on 12 Dec)
6: (2) ['2022-12-12', null]   ---> Remove due to "Reason 1"
7: (2) ['2022-12-12', null]   ---> Remove due to "Reason 1"

8: (2) ['2022-12-19', null]   ---> Remove due to "Reason 2"
9: (2) ['2022-12-19', null]   ---> Remove due to "Reason 2"
10: (2) ['2022-12-19', null]   ---> Remove due to "Reason 2"
11: (2) ['2022-12-19', null]   ---> Remain because no y value on this date

Reason 1: This member of the array should be removed because we have a null value on the same date (as specified in the x).

0: (2) [x: Date, y: value]

Reason 2: This member should be removed because we have 4 duplicate objects with the same values we only need 1 of them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Expected result:

It would be an array with 3 values (sets).

0: (2) ['2022-12-05', 2695.923015841]   ---> Remain because it has a y value 
1: (2) ['2022-12-12', 3984.864626441]   ---> Remain because it has a y value 
2: (2) ['2022-12-19', null]   ---> Remain because no y value on this date

>Solution :

const arr = [
  ['2022-12-05', null],
  ['2022-12-05', 2695.923015841],
  ['2022-12-05', null],
  ['2022-12-05', null],
  ['2022-12-12', null],
  ['2022-12-12', 3984.864626441],
  ['2022-12-12', null],
  ['2022-12-12', null],
  ['2022-12-19', null],
  ['2022-12-19', null],
  ['2022-12-19', null],
  ['2022-12-19', null],
];

const newArr = [...new Set(arr.map(item => item[0]))].map(item => {
  const findVal = arr.find(_item => _item[0] === item && _item[1]);
  if (findVal) return findVal;
  return [item, null];
});

console.log(newArr);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading