简体   繁体   English

在 object javascript express 中过滤嵌套数组

[英]Filter nested array in object javascript express

Considering the below object:考虑以下 object:

[
  {
    id: 5fc0be2990a8a12cc0ba0b5c,
    projectName: 'E-271120-B',
    projectManagaer: '5f7f1ba973ff621da4322248',
    dataInici: 2020-11-26T23:00:00.000Z,
    dataEntrega: 2020-11-26T23:00:00.000Z,
    dtoGlobal: null,
    dtoProjecte: null,
    archived: false,
    created: 2020-11-27T08:51:57.242Z,
    updated: 2021-01-25T10:01:18.733Z
    tabs: [{permissionsUserID:[250,8]},{permissionsUserID:[3]}],
    __v: 3
  },
  {
    tabs: [{permissionsUserID:[3,350]},{permissionsUserID:[15]}],
    _id: 5fc0be4690a8a12cc0ba0b5f,
    projectManagaer: '5f7f0e69b5862e1a085db388',
    projectName: 'E-271120-C',
    dataInici: 2020-11-27T23:00:00.000Z,
    dataEntrega: 2020-11-29T23:00:00.000Z,
    dtoGlobal: null,
    dtoProjecte: null,
    archived: false,
    created: 2020-01-21T08:46:41.958Z,
    updated: 2021-01-21T08:46:41.958Z,
    __v: 2
  },
  {
    tabs: [{permissionsUserID:[31,350]},{permissionsUserID:[8,893]}],
    _id: 5fc0be4690a8a12cc0ba0b5f,
    projectManagaer: '5f7f0e69b5862e1a085db388',
    projectName: 'E-23410-C',
    dataInici: 2020-11-27T23:00:00.000Z,
    dataEntrega: 2020-11-29T23:00:00.000Z,
    dtoGlobal: null,
    dtoProjecte: null,
    archived: false,
    created: 2020-01-21T08:46:41.958Z,
    updated: 2021-01-21T08:46:41.958Z,
    __v: 2
  }
]

Each object represents a Project.每个 object 代表一个项目。 A project has many tabs.一个项目有很多选项卡。

I want to return only the projects that at least one tab contains in permissionsUserID the ID of the user that is logged.我只想返回至少一个选项卡中包含的项目 permissionsUserID 记录的用户的 ID。

So if the user that is logged has the ID 8, these are the projects I want to obtain:因此,如果登录的用户的 ID 为 8,则这些是我要获取的项目:

[
  {
    id: 5fc0be2990a8a12cc0ba0b5c,
    projectName: 'E-271120-B',
    projectManagaer: '5f7f1ba973ff621da4322248',
    dataInici: 2020-11-26T23:00:00.000Z,
    dataEntrega: 2020-11-26T23:00:00.000Z,
    dtoGlobal: null,
    dtoProjecte: null,
    archived: false,
    created: 2020-11-27T08:51:57.242Z,
    updated: 2021-01-25T10:01:18.733Z
    tabs: [{permissionsUserID:[250,8]},{permissionsUserID:[3]}],
    __v: 3
  },
{
    tabs: [{permissionsUserID:[31,350]},{permissionsUserID:[8,893]}],
    _id: 5fc0be4690a8a12cc0ba0b5f,
    projectManagaer: '5f7f0e69b5862e1a085db388',
    projectName: 'E-23410-C',
    dataInici: 2020-11-27T23:00:00.000Z,
    dataEntrega: 2020-11-29T23:00:00.000Z,
    dtoGlobal: null,
    dtoProjecte: null,
    archived: false,
    created: 2020-01-21T08:46:41.958Z,
    updated: 2021-01-21T08:46:41.958Z,
    __v: 2
  }
]

That's the filter I have done:这就是我所做的过滤器:

async getAll(pagination, user) {
    try {
      const filter = {};
      if(pagination.archived) {
        filter['archived'] = pagination.archived;
      }
      if(pagination.search) {
        filter['$text'] = {$search: pagination.search}
      }

      const { Project: projectSchema } = this.getSchemas();
    

      const projectsDocs = await projectSchema.paginate(filter, {
        limit: pagination.limit ? parseInt(pagination.limit) : 10,
        page: pagination.page ? parseInt(pagination.page) + 1 : 1
      });

      if (!projectsDocs) {
        throw new errors.NotFound('No Projects.');
      }

      projectsDocs.docs.forEach(element => {
        element.tabs.filter( d => d.permissionsUserID.every( c => c.includes(user._id)));
      });

      return projectsDocs;
    } catch (error) {
      throw error;
    }
},

Here is one way这是一种方法

const data = [...];
const userId = 8;
const result = data.filter((item) => {
    const {tabs} = item;
    let loggedIn = false;
    tabs.forEach((tab) => {
        if (tab.permissionsUserID.includes(userId)) {
            loggedIn = true;
            return true
        }
    })
    return loggedIn;
})

Here's a simple function which should get you what you want.这是一个简单的 function,它应该可以满足您的需求。

Filter() returns a subset of the projects list. Filter()返回项目列表的子集。 Some() returns true if at least one of the tabs has the value we're looking for.如果至少有一个选项卡具有我们要查找的值,则Some()返回 true。 Includes() returns true if the permissionsUserId list has the user id we want.如果 permissionsUserId 列表具有我们想要的用户 ID,则Includes()返回 true。 Chain those together and you get the subset of projects where a tab's permissions has the desired user id.将它们链接在一起,您将获得选项卡权限具有所需用户 ID 的项目子集。

const data = [
        /* list of projects */
    ],
    userId = 8;

function getProjectsForUserId (data, userId) {
    return data.filter((project) => {
        return project.tabs.some((tab) => {
            return tab.permissionsUserID.includes(userId);
        });
    });
}

console.log(getProjectsForUserId(data, 8));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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