可以利用某人的专业知识按特定值过滤我的嵌套数组

Could use someone's expertise with filtering my nested array by a certain value

我知道有很多“如何过滤我的数组”的问题。但是,我还是卡住了。

我对数组不太熟悉,希望有人能帮助我解决我的特殊情况。

我有一个要过滤的数组。如果“user.name”或“writers.name”等于“Adam”,我正在寻找 return 所有数据。 “用户”永远只有一个名字,而“作者”可以有多个。

    [{
     mediaId: '86699',
     user: {
       name: 'Adam',
       id: '17622'
     },
     nodeType: 'testNode',
     title: 'testTitle',
     id: '23968',
     writers: []
   },
   {
     mediaId: '90547',
     user: {
       name: 'Jake',
       id: '65936'
     },
     nodeType: 'testNode',
     title: 'testTitleTwo',
     id: '89960',
     writers: [{
         name: 'Adam',
         id: '17622'
       },
       {
         name: 'Steve',
         id: '47622'
       }]
   },
   {
     mediaId: '99662',
     user: {
       name: 'James',
       id: '22236'
     },
     nodeType: 'testNode',
     title: 'testTitleThree',
     id: '89960',
     writers: [{
       name: 'Paul',
       id: '27622'
     }]
   }
 ]

期望的输出:

[{
    "mediaId": "86699",
    "user": {
      "name": "Adam",
      "id": "17622"
    },
    "nodeType": "testNode",
    "title": "testTitle",
    "id": "23968",
    "writers": []
  },
  {
    "mediaId": "90547",
    "user": {
      "name": "Jake",
      "id": "65936"
    },
    "nodeType": "testNode",
    "title": "testTitleTwo",
    "id": "89960",
    "writers": [{
        "name": "Adam",
        "id": "17622"
      },
      {
        "name": "Steve",
        "id": "45389"
      }]
  }
]

提前致谢!!!

更新: 除了用户和作者现在围绕属性外,一切都一样。

[{
   mediaId: '86699',
   nodeType: 'testNode',
   title: 'testTitle',
   id: '23968',
   properties: {
     user: {
       name: 'Adam',
       id: '17622'
     },
     writers: []
   }
 }, {
   mediaId: '90547',
   nodeType: 'testNode',
   title: 'testTitleTwo',
   id: '89960',
   properties: {
     user: {
       name: 'Jake',
       id: '65936'
     },
     writers: [{
       name: 'Adam',
       id: '17622'
     }, {
       name: 'Steve',
       id: '47622'
     }]
   }
 }, {
   mediaId: '99662',
   nodeType: 'testNode',
   title: 'testTitleThree',
   id: '89960',
   properties: {
     user: {
       name: 'James',
       id: '22236'
     },
     writers: [{
       name: 'Paul',
       id: '27622'
     }]
   }
 }]
input.filter(element => element.user.name === 'Adam' || element.writers.some(writer => writer.name === 'Adam'));

要应用过滤器,您实际上是在遍历数组中的每个项目并查看它是否符合您的条件。 Return true 如果您希望它保留,return false 如果您希望它被过滤掉。

因此在 top-most 层,您有一个对象数组,每个对象都代表一个节点(根据“节点类型”属性 判断)。我们从这个开始:

const matches = array.filter(node => {
  // criteria go here
  return true; // or false
});

标准是什么?那么它应该保留如果...

  1. node.user.name 是“亚当”
  2. 在作家中,至少有一位 writer.name === “亚当”

让我们来定义它们。

const hasUserNameAdam = (node) => node?.user?.name === "Adam";
const hasWriterNameAdam = (writer) => writer?.name === "Adam";

我们可以对 writer 数组做同样的事情来处理第二层。

const arrayHasWriterNameAdam = writerArray.some(writer => hasWriterNameAdam(writer));

现在我们只需要将它们插入即可。

const matches = data.filter(node => {
  const hasUserNameAdam = (node) => node?.user?.name === "Adam";
  const hasWriterNameAdam = (writer) => writer?.name === "Adam";
  const arrayHasWriterNameAdam = (writerArray) => writerArray.some(writer => hasWriterNameAdam(writer));

  return hasUserNameAdam(node) || arrayHasWriterNameAdam(node?.writers);
});

如果我们愿意,我们可以选择将这些内部函数重构回直线以进行清理。

const matches = data.filter(node => node?.user?.name === "Adam" || node?.writers?.some(writer => writer?.name === "Adam"));

更新

或者使用更新后的对象形状,userwriters 道具嵌套在 properties 道具下,所以我们只需要添加 .properties 层我们在任何地方访问 userwriters 属性以适应。

const matches = data.filter(node => node?.properties?.user?.name === "Adam" || node?.properties?.writers?.some(writer => writer?.name === "Adam"));

试试这个:

const findAdam = arr => arr.filter(
element => element.properties.user.name === 'Adam' || element.properties.writers.some(writer => writer.name === 'Adam')
)