【问题标题】:Filter out array of objects by specific values and keys defined in another array通过在另一个数组中定义的特定值和键过滤出对象数组
【发布时间】:2021-05-25 10:49:42
【问题描述】:

我有这些数据:

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}, 
]

我想通过键和值过滤这个数组:

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

所以我确实想要在animal 键中具有'' || null || undefined 值的记录,并且在status 键中既没有具有-1 || null || undefined 的行。

所以结果应该是:

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}, 
]

我正在开始创建这个函数,但我不知道如何继续:

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

const filtered = filterData()

我的问题与How to filter array when object key value is in array 类似,但有点复杂

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    您可以通过检查属性来过滤对象。

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

    【讨论】:

    • 这个答案提供了问题所需的灵活性,灵活解释MANDATORY_FIELDS数组
    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2019-07-18
    • 2022-07-12
    • 2021-10-30
    • 1970-01-01
    • 2019-08-20
    • 2020-01-27
    相关资源
    最近更新 更多