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

103
Visualizações
keep objects in an array

How can I store objects in an array so that I can call a method for each object in a loop.

How I would like the result to be:

[[obj1, obj2],[obj3, obj4]].foreach((firstObj, secondObj) => {
   firstobj.search();
   secondObj.search()
})

And the result will be as follows:

Iteration 1: obj1.search(); obj2.search();

iteration 2: obj3.search(); obj4.search();

Any advice is welcome

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

0

Use array destructuring in the argument list to "unpack" the pairs to 2 free variables:

[[obj1, obj2], [obj3, obj4]].forEach(([o1, o2]) => {
   o1.search();
   o2.search();
});

This is equivalent to doing (and indeed, this is what Babel compiles the above to)

[
  [obj1, obj2],
  [obj3, obj4]
].forEach((_ref) => {
  let [o1, o2] = _ref;
  o1.search();
  o2.search();
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You can unpack your array into two separate variables in the callback arguments like this.

[[obj1, obj2],[obj3, obj4]].forEach([firstObj, secondOb] => {
    firstobj.search();
    secondObj.search()
})

Or if you don't know the size of each nested array you can use flat to flatten your array into one then execute your function in a forEach.

[[obj1, obj2],[obj3, obj4]].flat().forEach(obj => {
    obj.search();
})
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