1

I've this data:

const data = [ 
  {animal: 'cat', name: 'mu', date: new Date(2020, 0, 1), status: -1},
  {animal: 'cat', name: 'muji', date: new Date(2021, 0, 1), status: 0},
  {animal: 'cat', name: 'mine', date: new Date(2021, 0, 1), status: 1},
  {animal: 'dog', name: 'fido', date: new Date(2021, 0, 1), status: 1}, 
  {animal: 'dog', name: 'fido2', date: new Date(2020, 0, 1), status: 1}, 
  {animal: 'dog', name: 'fido3', date: new Date(2021, 0, 1), status: 0}, 
  {animal: 'hamster', name: 'gerry', date: new Date(2019, 0, 1), status: 0}, 
  {animal: 't-rex', name: 'dino', date: new Date(2020, 0, 1), status: 0},
  {animal: null, name: 'sauro', date: new Date(2019, 0, 1), status: 0},
  {animal: 'sheep', name: 's', date: new Date(2019, 0, 1), status: 0}, 
  {animal: 'sheep', name: 'sss', date: new Date(2019, 0, 1), status: -1}, 
]

and I would like to filter this array by keys and values:

const MANDATORY_FIELDS = [{col:'animal', v: ['', null, undefined]}, {col:'status', v: [-1, null, undefined]}]

So I did'w want the record that have value '' || null || undefined in animal key and neither rows with -1 || null || undefined in status key.

So the result should be:

const data = [ 
  {animal: 'cat', name: 'muji', date: new Date(2021, 0, 1), status: 0},
  {animal: 'cat', name: 'mine', date: new Date(2021, 0, 1), status: 1},
  {animal: 'dog', name: 'fido', date: new Date(2021, 0, 1), status: 1}, 
  {animal: 'dog', name: 'fido2', date: new Date(2020, 0, 1), status: 1}, 
  {animal: 'dog', name: 'fido3', date: new Date(2021, 0, 1), status: 0}, 
  {animal: 'hamster', name: 'gerry', date: new Date(2019, 0, 1), status: 0}, 
  {animal: 't-rex', name: 'dino', date: new Date(2020, 0, 1), status: 0},
  {animal: 'sheep', name: 's', date: new Date(2019, 0, 1), status: 0}, 
]

I'm starting create this function but I don't know how to continue:

function filterData() {
 return data.filter((datum) => {
    return ??
  }) 
}

const filtered = filterData()

My problem is similar to How to filter array when object key value is in array but a bit more complex

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
whitecircle
  • 237
  • 9
  • 34

1 Answers1

4

You could filter the object with a check of the properties.

const
    data = [{ animal: 'cat', name: 'mu', date: new Date(2020, 0, 1), status: -1 }, { animal: 'cat', name: 'muji', date: new Date(2021, 0, 1), status: 0 }, { animal: 'cat', name: 'mine', date: new Date(2021, 0, 1), status: 1 }, { animal: 'dog', name: 'fido', date: new Date(2021, 0, 1), status: 1 }, { animal: 'dog', name: 'fido2', date: new Date(2020, 0, 1), status: 1 }, { animal: 'dog', name: 'fido3', date: new Date(2021, 0, 1), status: 0 }, { animal: 'hamster', name: 'gerry', date: new Date(2019, 0, 1), status: 0 }, { animal: 't-rex', name: 'dino', date: new Date(2020, 0, 1), status: 0 }, { animal: null, name: 'sauro', date: new Date(2019, 0, 1), status: 0 }, { animal: 'sheep', name: 's', date: new Date(2019, 0, 1), status: 0 }, { animal: 'sheep', name: 'sss', date: new Date(2019, 0, 1), status: -1 }],
    MANDATORY_FIELDS = [{ col: 'animal', v: ['', null, undefined] }, { col: 'status', v: [-1, null, undefined] }],
    result = data.filter(o =>
        !MANDATORY_FIELDS.some(({ col, v }) => v.includes(o[col]))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • This answer provides the flexibility required in the question, flexibly interpreting the `MANDATORY_FIELDS` array – fravolt May 25 '21 at 10:59