Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
How to optimize this method(Find an item with two searches)

i have that Object with two lists inside.

const list = {
    Operator: [
      {
        Login: 'login1',
        PermissionGroup: '2',
        Sub: '3',
      },
      {
        Login: 'login2',
        PermissionGroup: '3',
        Sub: '2',
      },
    ],
    PermissionGroups: [
      {
        PermissionGroupId: '2',
        GroupsName: 'Gestor',
        PermissionLevel: 2,
      },
      {
        PermissionGroupId: '3',
        GroupsName: 'Lider',
        PermissionLevel: 3,
      },
    ],
  };

In my redux, i have the "Sub" of the Operators. So what do I have to do...

Step 1: Get my Redux "sub" and find which Operator it is in

Step 2: Now that I have the Operator, I need to find which operator corresponds to the PermissionGroup, using the PermissioninGroup(into Operators) and the PermissionGroupId(into PermissionGroups)

This method does what I want (Ignore final return), How do I optimize this method? I didn't want to use two filters for this

const search = (sub) => {
    const usuario = listaUsuarios.filter((element) => element.Sub === sub)[0];

    const permissaoUsuario = listaPermissoes.filter(
      (element) => element.PermissionGroupId === usuario.PermissionGroup
    )[0];

    return permissaoUsuario.PermissionLevel;
  };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Personally, the current solution seems okay (of course, if it is confirmed that one only requires the 0-th element from .filter(), it may be preferrable to use .find() instead). Since OP has requested an alternate, the solution below (which is not my recommendation) may achieve the desired objective.

Code Snippet

const searchSub = (needle, {Operator, PermissionGroups}) => (
  [...Operator, ...PermissionGroups].reduce(
    (res, obj) => ({
      ...res,
      ...(
        'Sub' in obj && obj.Sub === needle
        ? {foundSub: {...obj}}
        : 'PermissionGroup' in res.foundSub &&
          'PermissionGroupId' in obj &&
          obj.PermissionGroupId === res.foundSub.PermissionGroup
          ? {matchedPermissionGroup: {...obj}}
          : {}
      )
    }),
    {foundSub: {}, matchedPermissionGroup: {}}
  )
);

const list = {
  'Operator': [{
      'Login': 'login1',
      'PermissionGroup': '2',
      'Sub': '3',
    },
    {
      'Login': 'login2',
      'PermissionGroup': '3',
      'Sub': '2',
    }
  ],
  'PermissionGroups': [{
      'PermissionGroupId': '2',
      'GroupsName': 'Gestor',
      'PermissionLevel': 2,
    },
    {
      'PermissionGroupId': '3',
      'GroupsName': 'Lider',
      'PermissionLevel': 3,
    },
  ]
};

console.log(searchSub('2', list))
console.log(
  'PermissionLevel: ',
  searchSub('2', list)?.matchedPermissionGroup.PermissionLevel
);

Explanation

  • Use ... spread operator to construct one array with elements from both Operator and PermissionGroups array
  • Use .reduce() to iterate over this combined array
  • When a Sub is found, track it in res object
  • When a matching PermissionGroupId is found, track that as well
  • The resulting res object will have elements from both Operator and PermissionGroups array that matched.

NOTE

  • Just to reiterate, it is actually simpler to just use two separate .find() in this context.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!