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

1 Answer 1

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

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

1 Comment

This answer provides the flexibility required in the question, flexibly interpreting the MANDATORY_FIELDS array

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.