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

164
Visualizações
How can I make my Javascript .map functions more efficient?

I'm currently returning a large set of data from AWS OpenSearch into an array called eventList. Each event within the array has the following structure:

{
  _source: {
    "key": value
    "key": value
    "key": value
  }
}

I therefore want to destructure this so that _source becomes the event data and then map over eventList. Currently, I've got two .map instances, and I'm aware this isn't that efficient, so I'd like to know how to combine these.

let eventList = [
  {
    _source: {
      tags: "xxx,yyy,zzz"
      ...
    }
  }
];

eventList = eventList.map((event) => event._source);
eventList = eventList.map((event) => {
  const { tags } = event;

  return {
    ...event,
    tags: tags?.split(",") ?? [],
  };
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As you say in the question, you can combine those two map calls:

eventList = eventList.map(({_source}) => (
    ..._source,
    tags: _source.tags?.split(",") ?? [],
));

I've used destructuring in the parameter list, but you don't have to do that if you don't want:

eventList = eventList.map((event) => (
    ...event._source,
    tags: event._source.tags?.split(",") ?? [],
));

There are a hundred different ways to write that, but you get the idea.

Actually, we could take the destructuring further:

eventList = eventList.map(({_source: {tags, ...rest}}) => (
    ...rest,
    tags: tags?.split(",") ?? [],
));

But all of these variations should be roughly the same in terms of performance; the big win is having just one map.

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