通过多个匹配条件递归过滤无限嵌套对象的数组,但仅返回具有两个匹配实例的父级

Dup*_*Dup 3 javascript arrays recursion filter multidimensional-array

我有以下对象数组;然而,这可以是任何未知的键/值并且可以无限嵌套,目前这是一个测试示例:

[
  {
    "reference_id": "R123",
    "customer": "Person 1",
    "customer_email": "person1@email.com",
    "location": "UK",
    "bookings": [
      {
        "product": "Product 1",
        "provider": "Company 1",
        "cancellable": true
      },
      {
        "product": "Product 2",
        "provider": "Company 2",
        "cancellable": true
      },
      {
        "product": "Product 3",
        "provider": "Company 1",
        "cancellable": true
      }
    ]
  },
  {
    "reference_id": "R1234",
    "customer": "Person 2",
    "customer_email": "person2@email.com",
    "location": "USA",
    "bookings": [
      {
        "product": "Product 1",
        "provider": "Company 1",
        "cancellable": true
      },
      {
        "product": "Product 3",
        "provider": "Company 1",
        "cancellable": true
      }
    ]
  },
  {
    "reference_id": "R12345",
    "customer": "Person 3",
    "customer_email": "person3@email.com",
    "location": "UK",
    "bookings": [
      {
        "product": "Product 2",
        "provider": "Company 2",
        "cancellable": true
      },
      {
        "product": "Product 3",
        "provider": "Company 1",
        "cancellable": true
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

我目前的实现如下:

const selected = [
  {
    term: 'Company 1',
    column: 'provider',
  },
  {
    term: 'Person 1',
    column: 'customer',
  },
];

const recursivelyFilterByValue = () => (value) => selected.every((item) => {
  if (!value) return false;

  if (typeof value === 'string') {
    // console.log('value', value === item.term);
    return value === item.term;
  }

  if (Array.isArray(value)) {
    return value.some(this.recursivelyFilterByValue());
  }

  if (typeof value === 'object') {
    return Object.values(value).some(this.recursivelyFilterByValue());
  }

  return false;
});

const results = data.filter(recursivelyFilterByValue());
Run Code Online (Sandbox Code Playgroud)

基本上,我添加到“选定”数组,然后使用它来过滤数据数组。我确实想确保密钥也与“列”匹配,但我还没有添加它。

对于上面的输入,我希望输出以下内容:

[
  {
    "reference_id": "R123",
    "customer": "Person 1",
    "customer_email": "person1@email.com",
    "location": "UK",
    "bookings": [
      {
        "product": "Product 1",
        "provider": "Company 1",
        "cancellable": true
      },
      {
        "product": "Product 2",
        "provider": "Company 2",
        "cancellable": true
      },
      {
        "product": "Product 3",
        "provider": "Company 1",
        "cancellable": true
      }
    ]
  },
]
Run Code Online (Sandbox Code Playgroud)

但是输出数组是空的。如果我只搜索一个术语(从所选数组中删除除一个术语之外的所有术语),则该术语的输出是正确的,但任何后续术语都会返回一个空白数组。

我想知道我对 .some() 的使用是否是问题所在,但是更改它会导致太多递归错误。

本质上,我想返回原始父对象,只要在其子对象的任何级别上,所选数组中的所有条件都有一个键:值匹配。

任何指导将不胜感激,谢谢。

Sco*_*yet 5

我不太确定这是否是您要找的。它假设我在评论中的猜测是正确的:

我有这个权利吗?您有一个(大概是动态的)条件,表明一个对象要么具有provider具有值的属性"Customer 1",要么具有具有该值的(递归)后代对象。您还有关于customer和 的第二个条件"Person 1",并且您正在寻找满足这两个(或所有)这些条件的对象。这是否描述了您想要做的事情?

这里我们有两个相当简单的辅助函数testRecursive以及makePredicates主函数recursivelyFilterByValue

const testRecursive = (pred) => (obj) => 
  pred (obj) || Object (obj) === obj && Object .values (obj) .some (testRecursive (pred))

const makePredicates = (criteria) => 
  criteria .map (({term, column}) => (v) => v [column] == term)

const recursivelyFilterByValue = (criteria, preds = makePredicates (criteria)) => (xs) =>
  xs .filter (obj => preds .every (pred => testRecursive (pred) (obj)))


const selected = [{term: 'Company 1', column: 'provider'}, {term: 'Person 1', column: 'customer'}]

const input = [{reference_id: "R123", customer: "Person 1", customer_email: "person1@email.com", location: "UK", bookings: [{product: "Product 1", provider: "Company 1", cancellable: true}, {product: "Product 2", provider: "Company 2", cancellable: true}, {product: "Product 3", provider: "Company 1", cancellable: true}]}, {reference_id: "R1234", customer: "Person 2", customer_email: "person2@email.com", location: "USA", bookings: [{product: "Product 1", provider: "Company 1", cancellable: true}, {product: "Product 3", provider: "Company 1", cancellable: true}]}, {reference_id: "R12345", customer: "Person 3", customer_email: "person3@email.com", location: "UK", bookings: [{product: "Product 2", provider: "Company 2", cancellable: true}, {product: "Product 3", provider: "Company 1", cancellable: true}]}]

console .log (recursivelyFilterByValue (selected) (input))
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {max-height: 100% !important; top: 0}
Run Code Online (Sandbox Code Playgroud)

  • testRecursive检查谓词对于对象或嵌套在其中的任何对象是否为真。

  • makePredicates将对象数组转换{term, column}为谓词函数,用于测试对象在由列命名的属性中是否具有正确的术语。

  • recursivelyFilterByValue将这些组合起来,调用makePredicates将所选项目转换为谓词函数,然后通过测试每个谓词是否为真来过滤输入。

这不是可以想象的最有效的代码。它重新扫描每个谓词的层次结构。我确信我们可以找出一个只扫描一次的版本,但我认为这会产生更复杂的代码。因此,您可能想要测试生产规模的数据是否足够快以满足您的需求。

  • 问题需要更多说明,但我很确定你已经解决了它 (2认同)