简体   繁体   中英

Filter out array of objects by specific values and keys defined in another array

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 '' || null || undefined '' || null || undefined in animal key and neither rows with -1 || null || undefined -1 || null || undefined -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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM