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

88
Visualizações
Nest each element of an array

I need my array of const arr = [a, b, c, d, e, f] to be nested, more specifically, every element to be so. Meaning my desired effect is for the array to looks like this arr = [[a], [b], [c], [d], [e], [f]]

For the opposite of what I want to achieve, I could use arr.flat(), but that's not the case here. So I'm looking for answers.

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

0

You want to perform something for every element of an array (in particular wrapping it inside another array). For performing an operation on every element of an array you can use Array.map().

const arr = [1, 2, 3];
const newArr = arr.map(item => [item]);
// newArr is [[1], [2], [3]]
about 4 years ago · Juan Pablo Isaza Relatório

0

This is an easy transform process like this one:

const arr = [a, b, c, d, e, f];
const arr2 = arr.map(item => [item]);
about 4 years ago · Juan Pablo Isaza Relatório

0

Just map over the array using a function that wraps the element in an array of 1 element:

(Note: the following snippet puts strings in the starting array just so that it runs without a ReferenceError. In practice this will work fine with any values, including variables that you have defined elsewhere in your file/script.)

const arr = ["a", "b", "c", "d", "e", "f"];

const newArr = arr.map(el => [el]);

console.log(newArr);

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