Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
Using one array filter instead of multiple ones

Is there a way to merge all these filters into one? Or another way to make it more effective? Or just use for loop?

 driving += value.filter((obj) => obj.type === CalendarEventType.MIND && obj.data.practice === 'driving').length;
    breathWork += value.filter((obj) => obj.type === CalendarEventType.MIND && obj.data.practice === 'breath work').length;
    meditation += value.filter((obj) => obj.type === CalendarEventType.MIND && obj.data.practice === 'meditation').length;
    cooking += value.filter((obj) => obj.type === CalendarEventType.MIND && obj.data.practice === 'cooking').length;
    walking += value.filter((obj) => obj.type === CalendarEventType.MIND && obj.data.practice === 'walking').length;
    other += value.filter((obj) => obj.type === CalendarEventType.MIND && obj.data.practice === 'other').length;

They look to me very redudant.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Currently you're looping 6 times over the same array. You can reduce the runtime with a simple for-loop or reduce:

const counts = value.reduce((acc, el) => {
  if (el.type !== CalendarEventType.MIND) return acc;
  acc[el.data.practice] = (acc[el.data.practice] ?? 0) + 1
  return acc;
}, {});

Example:

const CalendarEventType = {
  MIND: 1,
  OTHER: 2
};

const value = [
  {type: CalendarEventType.MIND, data: { practice: 'driving' } },
  {type: CalendarEventType.MIND, data: { practice: 'breath work' } },
  {type: CalendarEventType.OTHER, data: { practice: 'breath work' } },
  {type: CalendarEventType.MIND, data: { practice: 'other' } },
  {type: CalendarEventType.MIND, data: { practice: 'other' } },
  {type: CalendarEventType.OTHER, data: { practice: 'other' } },
];

const counts = value.reduce((acc, el) => {
  if (el.type !== CalendarEventType.MIND) return acc;
  acc[el.data.practice] = (acc[el.data.practice] ?? 0) + 1
  return acc;
}, {});

console.log(counts);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could take an array of values and use a single filter.

const
    practices = ['driving', 'breath work', 'meditation', 'cooking', 'walking', 'other'];
    filter = ({ type, data: { practice } }) => type === CalendarEventType.MIND && practices.includes(practice),
    result = value.filter(filter),
    count = values.reduce((sum, o) => sum + filter(o), 0);
about 4 years ago · Juan Pablo Isaza Relatório

0

One possible way would be to group by data.practice. Since there's no native "group by" function, you could implement it yourself (as described here), or you could use a library like Lodash or Ramda.

Example, using Lodash's groupBy:

const filteredItems = value.filter((obj) => obj.type === CalendarEventType.MIND);
const groupedItems = groupBy(filteredItems, obj => obj.data.practice);

Then you can do whatever you want with groupedItems, e.g.:

driving += groupedItems['driving'] ? groupedItems['driving'].length : 0;

Note that if a given value for data.practice is not represented in filteredItems, then that key won't be present in groupedItems - hence the need to check for that key.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda