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