Home > database >  How to remove duplicates from array if value set for one of the duplicates, else hold null (javascri
How to remove duplicates from array if value set for one of the duplicates, else hold null (javascri

Time:12-29

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.

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

CodePudding user response:

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

CodePudding user response:

You could reduce the array with a single loop and check if date

const
    data = [['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]],
    result = data.reduce((r, [date, value], i, a) => {
        if (i && a[i - 1][0] === date) r.at(-1)[1] ||= value;
        else r.push([date, value]);
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related